Running JUnit tests with Maven under Eclipse

强颜欢笑 提交于 2019-12-18 02:41:49

问题


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

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.

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

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.

Thanks!


回答1:


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.

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

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




回答2:


Add this to your 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



来源:https://stackoverflow.com/questions/5451808/running-junit-tests-with-maven-under-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!