有时候我们需要按照顺序执行我们的单元测试方法,如在测试Dao层的时候要按照测试插入方法、查询方法、更新方法、删除方法的顺序测试。如果不按照这个顺序测试可能会出现问题,而Junit测试时默认的顺序是随机的。如果插入方法在后面执行可能前面的方法都不能通过测试,因为数据还没有插入。
下面是Dao层的方法
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 1package com.tiamaes.bean.dao;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.data.mongodb.core.MongoTemplate;
5import org.springframework.stereotype.Repository;
6
7import com.tiamaes.bean.GjdsBusSite;
8
9@Repository
10public class GjdsBusSiteDao {
11
12 @Autowired
13 private MongoTemplate mongoTemplate;
14 public GjdsBusSite get(String id){
15 GjdsBusSite bean = this.mongoTemplate.findById(id, GjdsBusSite.class);
16 return bean;
17 }
18
19 public void save(GjdsBusSite bean){
20 this.mongoTemplate.save(bean);
21 }
22
23 public void update(GjdsBusSite bean){
24
25 }
26
27 public void remove(String id){
28 GjdsBusSite bean = this.get(id);
29 this.mongoTemplate.remove(bean);
30 }
31}
32
33
1
2 1下面是测试
2
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
78
79
80 1package com.tiamaes.dao;
2
3import static org.junit.Assert.fail;
4
5import java.util.Date;
6
7import static org.junit.Assert.*;
8
9import org.junit.Before;
10import org.junit.FixMethodOrder;
11import org.junit.Ignore;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.junit.runners.MethodSorters;
15import org.springframework.beans.factory.annotation.Autowired;
16import org.springframework.test.context.ContextConfiguration;
17import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18
19import com.tiamaes.bean.GjdsBusSite;
20import com.tiamaes.bean.dao.GjdsBusSiteDao;
21
22@RunWith(SpringJUnit4ClassRunner.class)
23@ContextConfiguration("classpath:applicationContext.xml")
24@FixMethodOrder(MethodSorters.DEFAULT)
25public class GjdsBusSiteDaoTest {
26 private GjdsBusSite bean = new GjdsBusSite();
27 //private String id = UUID.randomUUID().toString();
28 private String id = "57706cd4-244e-4101-a317-ee34347c4168";
29
30 @Autowired
31 private GjdsBusSiteDao gjdsBusSiteDao;
32
33
34 @Before
35 public void init(){
36 System.out.println(id);
37 bean.setId(id);
38 bean.setBusNo("125412");
39 bean.setLat(123.1111);
40 bean.setLng(24.1111);
41 bean.setSiteTime(new Date());
42 bean.setLineNo("B1");
43 }
44
45 @Test
46 public void testDao(){
47 assertNotNull(this.gjdsBusSiteDao);
48 }
49 @Test
50 public void testSave() {
51 this.gjdsBusSiteDao.save(bean);
52
53 }
54
55 @Test
56 public void testGet() {
57 GjdsBusSite entity = this.gjdsBusSiteDao.get(id);
58 assertNotNull(entity);
59 assertNotNull(id,entity.getId());
60 }
61
62 @Test
63 @Ignore
64 public void testUpdate() {
65 fail("Not yet implemented");
66 }
67
68 @Test
69 @Ignore
70 public void testRemove() {
71 //fail("Not yet implemented");
72 this.gjdsBusSiteDao.remove(id);
73 GjdsBusSite e = this.gjdsBusSiteDao.get(id);
74 assertNull(e);
75
76 }
77
78}
79
80
1
2 1因为用到了Spring所以需要将Runner指定为SpringJUnit4ClassRunner并使用@ContextConfiguration指定配置文件。
2
@Before用来初始化Bean,在每个方法执行时都会执行一次
@Ignore指定忽略此测试方法
@FixMethodOrder用来指定方法执行顺序,传入一个MethodSorters
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 1package org.junit.runners;
2
3import java.lang.reflect.Method;
4import java.util.Comparator;
5
6import org.junit.internal.MethodSorter;
7
8/**
9 * Sort the methods into a specified execution order.
10 * Defines common {@link MethodSorter} implementations.
11 *
12 * @since 4.11
13 */
14public enum MethodSorters {
15 /**
16 * Sorts the test methods by the method name, in lexicographic order,
17 * with {@link Method#toString()} used as a tiebreaker
18 */
19 NAME_ASCENDING(MethodSorter.NAME_ASCENDING),
20
21 /**
22 * Leaves the test methods in the order returned by the JVM.
23 * Note that the order from the JVM may vary from run to run
24 */
25 JVM(null),
26
27 /**
28 * Sorts the test methods in a deterministic, but not predictable, order
29 */
30 DEFAULT(MethodSorter.DEFAULT);
31
32 private final Comparator<Method> fComparator;
33
34 private MethodSorters(Comparator<Method> comparator) {
35 this.fComparator = comparator;
36 }
37
38 public Comparator<Method> getComparator() {
39 return fComparator;
40 }
41}
42
43
DEFAULT会按照方法的顺序执行
NAME_ASCENDING:对方法名升序排序,然后按照排序后的顺序执行