JUnit版本导致eclipse的build path加入的Maven Dependencies没起作用

使用eclipse创建了Maven webapp project。在写单元测试用例时,报错@Test注解找不到,提示将JUnit添加到build path中(Add  JUnit 4 library to the build path)

点击自动纠错后,可以看到eclipse将自带的JUnit4 jar包加入到了build path中。

但是其实在build path中的Maven Dependencies中是有依赖的JUnit的

为什么Maven Dependencies中已经有JUnit了,还是找不到@Test呢。

试了一段时间,怀疑是JUnit的版本导致,我们看到的Maven Dependencies确实含有JUnit,只不过版本是3.8.1,于是修改pom.xml,将JUnit的版本改为了4.12

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

这时候再看,就正常了。

原文地址:https://www.cnblogs.com/wendelhuang/p/6794845.html