简体   繁体   English

编译错误:package my.package.root.util 不存在

[英]COMPILATION ERROR : package my.package.root.util does not exist

I have an Eclipse Java project for which I am trying to execute the unit tests using Maven.我有一个 Eclipse Java 项目,我正在尝试使用 Maven 对其执行单元测试。

I have my unit tests as below so that it respects the expected hierarchy我有如下的单元测试,以便它尊重预期的层次结构

src/test/java/StringUtilsTests.java

However, my unit test references the source code located in:但是,我的单元测试引用了位于以下位置的源代码:

src/my/package/root/util/StringUtils.java

just because it has always been like this and I don't want to change my folder hierarchy for the tests.只是因为它一直都是这样,我不想为测试更改我的文件夹层次结构。

Therefore, I use the build-helper-maven-plugin to add this source folder, as below因此,我使用 build-helper-maven-plugin 来添加这个源文件夹,如下

 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                <source>src/my/package/root/util</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

But even with that, I get the below error:但即便如此,我还是收到以下错误:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/frederic/git/myproject/src/test/java/StringUtilsTests.java:[22,32] package package my.package.root.util does not exist

Below is my reference to the maven-surefire-plugin下面是我对 maven-surefire-plugin 的引用

<plugin>
              <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M5</version>
          <configuration>
              <skipTests>false</skipTests>
              </configuration>
          <executions>
              <execution>
                  <goals>
                      <goal>test</goal>
                  </goals>
                      </execution>
          </executions>
    </plugin>

And it's even worse if I don't put my test in如果我不进行测试,情况会更糟

src/test/java/StringUtilsTests.java

but instead here for example:但在这里例如:

src/tests/my/package/root/util/StringUtilsTests.java

The maven-surefire-plugin will show maven-surefire-plugin 将显示

No tests to run

Do you need to run them as Unit-Tests or can you run them as Integration-Tests?您需要将它们作为单元测试运行还是可以将它们作为集成测试运行? I think Integration tests would work out-of-the-box like this?我认为集成测试会像这样开箱即用吗? Simply change the class names to end with IT MyClassIT.java for these tests or finetune your.pom and change the goal to integration-test-phase or verification-phase?只需将 class 名称更改为以IT MyClassIT.java结尾即可进行这些测试,或者微调 your.pom 并将目标更改为集成测试阶段或验证阶段? I think that should work since it will be executed later on in the build cycle.我认为这应该可行,因为它将在构建周期的后期执行。

Otherwise try to change this line with a wildcard: <source>src/my/package/root/util/*</source>否则尝试使用通配符更改此行: <source>src/my/package/root/util/*</source>

Based on this article , adding the below elements to my pom.xml fixed my issue:根据这篇文章,将以下元素添加到我的pom.xml解决了我的问题:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <executions>
      <execution>
        <id>compiletests</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>


<testSourceDirectory>${project.basedir}/src-test</testSourceDirectory>

And VERY important was to add this dependency非常重要的是添加这个依赖

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM