且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven编译给出:找不到符号 - 对于坐在同一个应用程序中的一个类

更新时间:2023-11-18 17:56:46

使用maven,您的测试可以访问项目的所有源代码(src / main / java)和所有测试源代码(默认为src / test / java)



这里,您的个人资料休息将测试源目录定义为 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


It's a while since I met such puzzling issue. I'm having a class that references another one sitting in another package in the same application, that is, NOT in another jar archive file.

The including class is learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java

The included class is /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java

Under Eclipse there is no issue and no compilation error in the editor.

But running a Maven build gives a compilation error:

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

Here is the code of the including class:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
@ContextConfiguration(classes = {
    ApplicationConfiguration.class,
    WebSecurityConfig.class,
    WebConfiguration.class,
    WebTestConfiguration.class
})
public abstract class AbstractControllerTest {

This abstract test class is sitting under the acceptance test directory, which mandates the explicit activation of the -Pacceptance profile when running the Maven command.

The default profile does not run this acceptance test, but only some integration test.

One thing to note, is that this abstract class looks like the one abstract class used in the integration test.

Here is the including class of the integration test:

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 {

As you can see, it looks much like the other one.

I can also give the pom.xml file content, if it can help:

<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>

If you have any clue on this one, it'd be greatly appreciated.

With maven your tests can access to all the source code of your project (src/main/java) and all the test source code (default is src/test/java).

Here, your profile rest defines the test source directory as src/test/java/com/thalasoft/learnintouch/rest So, your test code can access everything in src/main/java and everything in src/test/java/com/thalasoft/learnintouch/rest

Your profile acceptance defines the test source directory as src/test/java/com/thalasoft/learnintouch/rest/acceptance So, your test code can access everything in src/main/java and everything in src/test/java/com/thalasoft/learnintouch/rest/acceptance. WebTestConfiguration is not accessible since it's in a package above.

To run specific tests with different profiles, I recommend to configure the surefire plugin in charge of running tests. A good example is available here : https://weblogs.java.net/blog/carcassi/archive/2011/04/21/running-integration-tests-and-unit-tests-separately-maven