自从我遇到这样令人费解的问题已经有一段时间了。我有一个类,该类引用另一个坐在同一个应用程序中的另一个包中的类,即不在另一个jar存档文件中。
包括的类是learnintouch-rest / src / test / java / com / thalasoft / learnintouch / rest / acceptance / AbstractControllerTest.java
包含的类是/home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java
在Eclipse下,编辑器中没有问题,也没有编译错误。
但是运行Maven构建会产生编译错误:
mvn clean test-compile -Pacceptance
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project learnintouch-rest: Compilation failure: Compilation failure:
[ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[16,46] cannot find symbol
[ERROR] symbol: class WebTestConfiguration
[ERROR] location: package com.thalasoft.learnintouch.rest.config
[ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[21,116] cannot find symbol
[ERROR] symbol: class WebTestConfiguration
这是包含类的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
@ContextConfiguration(classes = {
ApplicationConfiguration.class,
WebSecurityConfig.class,
WebConfiguration.class,
WebTestConfiguration.class
})
public abstract class AbstractControllerTest {
此抽象测试类位于接受测试目录下,该目录在运行Maven命令时要求显式激活-Pacceptance配置文件。
默认配置文件不运行此验收测试,而仅运行某些集成测试。
要注意的一件事是,该抽象类看起来像集成测试中使用的一个抽象类。
这是集成测试的包含类:
import com.thalasoft.learnintouch.rest.config.WebTestConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class},
WebSecurityConfig.class,
WebConfiguration.class,
WebTestConfiguration.class
})
@Transactional
public abstract class AbstractControllerTest {
如您所见,它看起来很像另一个。
如果有帮助,我还可以提供pom.xml文件的内容:
<profiles>
<profile>
<id>rest</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<test.source.dir>src/test/java/com/thalasoft/learnintouch/rest</test.source.dir>
</properties>
</profile>
<profile>
<id>acceptance</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<test.source.dir>src/test/java/com/thalasoft/learnintouch/rest/acceptance</test.source.dir>
</properties>
</profile>
</profiles>
如果您对此有任何了解,将不胜感激。
使用maven,您的测试可以访问项目的所有源代码(src / main / java)和所有测试源代码(默认为src / test / java)。
在这里,您的个人档案rest将测试源目录定义为src / test / java / com / thalasoft / learnintouch / rest,因此,您的测试代码可以访问src / main / java中的所有内容以及src / test / java / com / thalasoft中的所有内容/ learnintouch / rest
您的个人资料接受将测试源目录定义为src / test / java / com / thalasoft / learnintouch / rest / acceptance,因此,您的测试代码可以访问src / main / java中的所有内容以及src / test / java / com / thalasoft中的所有内容/ learnintouch / rest / acceptance。WebTestConfiguration不可访问,因为它在上述软件包中。
要使用不同的配置文件运行特定的测试,我建议配置负责运行测试的surefire插件。此处提供了一个很好的示例:https : //weblogs.java.net/blog/carcassi/archive/2011/04/21/running-integration-tests-and-unit-tests-separately-maven
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句