我好久没有遇到这么令人费解的问题了。我有一个类引用另一个位于同一应用程序的另一个包中的类,也就是说,不在另一个 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 中的所有内容
您的个人资料 acceptance 将测试源目录定义为 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
关于java - Maven 编译给出 : Cannot find symbol - For a class sitting in the same app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23327920/