Junit使用教程(二)

释放双眼,带上耳机,听听看~!

二、核心——断言

断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过。

  1. 断言核心方法
assertArrayEquals(expecteds, actuals) 查看两个数组是否相等。
assertEquals(expected, actual) 查看两个对象是否相等。类似于字符串比较使用的equals()方法
assertNotEquals(first, second) 查看两个对象是否不相等。
assertNull(object) 查看对象是否为空。
assertNotNull(object) 查看对象是否不为空。
assertSame(expected, actual) 查看两个对象的引用是否相等。类似于使用“==”比较两个对象
assertNotSame(unexpected, actual) 查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象
assertTrue(condition) 查看运行结果是否为true。
assertFalse(condition) 查看运行结果是否为false。
assertThat(actual, matcher) 查看实际值是否满足指定的条件
fail() 让测试失败

1
1
  1. 示例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
1package test;
2
3import static org.hamcrest.CoreMatchers.*;
4import static org.junit.Assert.*;
5
6import java.util.Arrays;
7
8import org.hamcrest.core.CombinableMatcher;
9import org.junit.Test;
10
11public class AssertTests {
12
13    @Test
14    public void testAssertArrayEquals() {
15      byte[] expected = "trial".getBytes();
16      byte[] actual = "trial".getBytes();
17      org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);
18    }
19
20    @Test
21    public void testAssertEquals() {
22      org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);
23    }
24
25    @Test
26    public void testAssertFalse() {
27      org.junit.Assert.assertFalse("failure - should be false", false);
28    }
29
30    @Test
31    public void testAssertNotNull() {
32      org.junit.Assert.assertNotNull("should not be null", new Object());
33    }
34
35    @Test
36    public void testAssertNotSame() {
37      org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
38    }
39
40    @Test
41    public void testAssertNull() {
42      org.junit.Assert.assertNull("should be null", null);
43    }
44
45    @Test
46    public void testAssertSame() {
47      Integer aNumber = Integer.valueOf(768);
48      org.junit.Assert.assertSame("should be same", aNumber, aNumber);
49    }
50
51    // JUnit Matchers assertThat
52    @Test
53    public void testAssertThatBothContainsString() {
54      org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
55    }
56
57    @Test
58    public void testAssertThathasItemsContainsString() {
59      org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
60    }
61
62    @Test
63    public void testAssertThatEveryItemContainsString() {
64      org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
65    }
66
67    // Core Hamcrest Matchers with assertThat
68    @Test
69    public void testAssertThatHamcrestCoreMatchers() {
70      assertThat("good", allOf(equalTo("good"), startsWith("good")));
71      assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
72      assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
73      assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
74      assertThat(new Object(), not(sameInstance(new Object())));
75    }
76}
77

三、核心——注解

  1. 说明
@Before 初始化方法
@After 释放资源
@Test 测试方法,在这里可以测试期望异常和超时时间
@Ignore 忽略的测试方法
@BeforeClass 针对所有测试,只执行一次,且必须为static void
@AfterClass 针对所有测试,只执行一次,且必须为static void
@RunWith 指定测试类使用某个运行器
@Parameters 指定测试类的测试数据集合
@Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为
@FixMethodOrder 指定测试方法的执行顺序

1
1
  1. 执行顺序

一个测试类单元测试的执行顺序为:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

每一个测试方法的调用顺序为:

@Before –> @Test –> @After

  1. 示例


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
1package test;
2
3import static org.junit.Assert.*;
4
5import org.junit.*;
6
7public class JDemoTest {
8
9   @BeforeClass
10  public static void setUpBeforeClass() throws Exception {
11      System.out.println("in BeforeClass================");
12  }
13
14  @AfterClass
15  public static void tearDownAfterClass() throws Exception {
16      System.out.println("in AfterClass=================");
17  }
18
19  @Before
20  public void before() {
21      System.out.println("in Before");
22  }
23
24  @After
25  public void after() {
26      System.out.println("in After");
27  }
28
29  @Test(timeout = 10000)
30  public void testadd() {
31      JDemo a = new JDemo();
32      assertEquals(6, a.add(3, 3));
33      System.out.println("in Test ----Add");
34  }
35
36  @Test
37  public void testdivision() {
38      JDemo a = new JDemo();
39      assertEquals(3, a.division(6, 2));
40      System.out.println("in Test ----Division");
41  }
42
43  @Ignore
44  @Test
45  public void test_ignore() {
46      JDemo a = new JDemo();
47      assertEquals(6, a.add(1, 5));
48      System.out.println("in test_ignore");
49  }
50
51  @Test
52  public void teest_fail() {
53      fail();
54  }
55}
56
57class JDemo extends Thread {
58
59  int result;
60
61  public int add(int a, int b) {
62      try {
63          sleep(1000);
64          result = a + b;
65      } catch (InterruptedException e) {
66      }
67      return result;
68  }
69
70  public int division(int a, int b) {
71      return result = a / b;
72  }
73}
74

执行结果:


1
2
3
4
5
6
7
8
9
1in BeforeClass================
2in Before
3in Test ----Add
4in After
5in Before
6in Test ----Division
7in After
8in AfterClass=================
9

 Junit使用教程(二)

图中左上红框中部分表示Junit运行结果,5个成功(1个忽略),1个错误,1个失败。(注意错误和失败不是一回事,错误说明代码有错误,而失败表示该测试方法测试失败)

左下红框中则表示出了各个测试方法的运行状态,可以看到成功、错误、失败、失败各自的图标是不一样的,还可以看到运行时间。

右边部分则是异常堆栈,可查看异常信息。

 

下篇中我们给出更多示例还继续介绍Junit

给TA打赏
共{{data.count}}人
人已打赏
安全技术

bootstrap栅格系统自定义列

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索