spring系列---junit测试

1、junit对应的jar包 

junit-4.12.jar 

2、使用junit报错,主要是版本的问题,高版本不包含有些功能

下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误,

上网查了一下,一般的解决方案是,换一个低一点的版本就好了。还有人说,是缺少hamcrest的包。去官网又看了一下,结果发现这样一段话:

  • junit.jar: Includes the Hamcrest classes. The simple all-in-one solution to get started quickly. Starting with version 4.11, Hamcrest is no longer included in this jar.
  • junit-dep.jar: Only includes the JUnit classes but not Hamcrest. Lets you use a different Hamcrest version.

解决办法是 

(1)换成junit-4.8.jar

(2)junit-4.12.jar + hamcrest-core-1.3.jar

3、junit 使用指导

import org.junit.Test; 之类的,就可以使用 @Test 来进行测试代码

  • @Test:把一个方法标记为测试方法
  • @Before:每一个测试方法执行前自动调用一次
  • @After:每一个测试方法执行完自动调用一次
  • @BeforeClass:所有测试方法执行前执行一次,在测试类还没有实例化就已经被加载,所以用static修饰
  • @AfterClass:所有测试方法执行完执行一次,在测试类还没有实例化就已经被加载,所以用static修饰
  • @Ignore:暂不执行该测试方法

猜你喜欢

转载自blog.csdn.net/hwang4_12/article/details/82950970