在Eclipse下使用Maven运行JUnit测试

时间:2023-02-05 10:35:47

I just installed the plugin m2e for the first time on Eclipse.

我第一次在Eclipse上安装了插件m2e。

I wrote a simple JUnit (version 4) test. I can run it from Eclipse, but not from the pom.xml (alt-click, Run as, Maven Test). I suppose I need to tell Maven to search for that class, but I just don't know how.

我编写了一个简单的JUnit(版本4)测试。我可以从Eclipse运行它,但不能从pom运行。xml (alt-click, Run as, Maven测试)。我想我需要告诉Maven去搜索那个类,但是我不知道怎么做。

Also, I couldn't find JUnit 4 in the groupId "junit": only the version 3.8.1 is available. Do I really need to write tests for version 3.x and not version 4+?

此外,我在不完善的“JUnit”中也找不到JUnit 4:只有3.8.1版本可用。我真的需要为版本3编写测试吗?x而不是版本4+?

How to fix this?

如何解决这个问题?

Think of me as a newbie with Maven: that's exactly what I am. So please don't speak about artifact technobabble unless describing exactly what I need to do. I could barely install Guava as dependency and I'm completely lost right now with these concepts.

把我当作一个与Maven打交道的新手:我就是这样的。所以请不要谈论伪技术,除非确切地描述我需要做什么。我几乎不能把番石榴作为依赖物来安装,现在我完全沉浸在这些概念中了。

Thanks!

谢谢!

2 个解决方案

#1


15  

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests.

您可以使用Maven运行Junit 4。您只需要pom中的Junit 4依赖项。您还需要surefire插件来执行测试。

See: http://maven.apache.org/plugins/maven-surefire-plugin/

参见:http://maven.apache.org/plugins/maven-surefire-plugin/

Hint: By default surefire looks for files with *Test.java naming to find tests.

提示:默认情况下,surefire查找带有*Test的文件。java命名以找到测试。

#2


1  

Add this to your pom.xml

将其添加到您的pom.xml中

<project>
  [...]
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.18.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  [...]
</project>

run maven with clean test

使用干净的测试运行maven

#1


15  

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests.

您可以使用Maven运行Junit 4。您只需要pom中的Junit 4依赖项。您还需要surefire插件来执行测试。

See: http://maven.apache.org/plugins/maven-surefire-plugin/

参见:http://maven.apache.org/plugins/maven-surefire-plugin/

Hint: By default surefire looks for files with *Test.java naming to find tests.

提示:默认情况下,surefire查找带有*Test的文件。java命名以找到测试。

#2


1  

Add this to your pom.xml

将其添加到您的pom.xml中

<project>
  [...]
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.18.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  [...]
</project>

run maven with clean test

使用干净的测试运行maven