使用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>
這時候再看,就正常了。

