单元测试典型场景

2019-11-19  本文已影响0人  梦想又照进现实

一、典型场景单元测试代码示例

场景一:模型内部测试

    private static final int ENTITY_NUM = 2;
    private static final String TEST_CMP_NAM_NULL = "";

    private BusIcsRtnDat busIcsRtnDat;
    private InputParamsDto inputDto;

    private String VAILATE_PASS_MSG = "";

    @Before
    public void setUp() {
        busIcsRtnDat = new BusIcsRtnDat();
        inputDto = new InputParamsDto();
        int inputDataSize = ENTITY_NUM;
        List<Map<String, String>> inputParamDatList =  new ArrayList<>();
        for (int i = 0; i < inputDataSize; i++) {
            Map<String, String> dataMap = new  HashMap();
            dataMap.put("appNbr","201909010000000".concat(i+TEST_CMP_NAM_NULL));
            dataMap.put("cmpNam","单位名称最大长度一百单位名称最大长度一百单位名称最大长度一百单位名称最大长度一百单位名称最大长度一");
            dataMap.put("chn_nam","张三");
            inputParamDatList.add(dataMap);
        }
        Map<String, Object> inputDtoMap = new HashMap();
        inputDtoMap.put("data", inputParamDatList);
        inputDto.setParams(inputDtoMap);
    }

    @Test
    public void give含有2条键档补件数据的报文_when调用报文转换实体方法_then会得到为2个数据库实体() {
        // given
        busIcsRtnDat.setInputDto(inputDto);

        // when
        List<IoBusIcsRtnDat> ioBusIcsRtnDats = busIcsRtnDat.convertInputToEntity();

        // then
        assertEquals(ENTITY_NUM, ioBusIcsRtnDats.size());
    }

}
 

场景二:需要Mock或者Spy对象用于配合当前模型测试

<pre style="margin: 0px; tab-size: 4; white-space: pre-wrap;">public class BusIcsRtnDatTest {

    private static final int ENTITY_NUM = 2;
    private static final String TEST_CMP_NAM_NULL = "";

    private BusIcsRtnDat busIcsRtnDat;
    private InputParamsDto inputDto;

    private String VAILATE_PASS_MSG = "";

    @Before
    public void setUp() {
        busIcsRtnDat = new BusIcsRtnDat();
        inputDto = new InputParamsDto();
        int inputDataSize = ENTITY_NUM;
        List<Map<String, String>> inputParamDatList =  new ArrayList<>();
        for (int i = 0; i < inputDataSize; i++) {
            Map<String, String> dataMap = new  HashMap();
            dataMap.put("appNbr","201909010000000".concat(i+TEST_CMP_NAM_NULL));
            dataMap.put("cmpNam","单位名称最大长度一百单位名称最大长度一百单位名称最大长度一百单位名称最大长度一百单位名称最大长度一");
            dataMap.put("chn_nam","张三");
            inputParamDatList.add(dataMap);
        }
        Map<String, Object> inputDtoMap = new HashMap();
        inputDtoMap.put("data", inputParamDatList);
        inputDto.setParams(inputDtoMap);
    }

    @Test
    public void give请求的报文中存在安全信息及收编数据_when进行安全性校验_then能获得为空的返回值(){
        // given
        //1.mock
        BusIcsRtnDat busIcsRtnDat = mock(BusIcsRtnDat.class);
        doReturn(VAILATE_PASS_MSG).when(busIcsRtnDat).validataParamsDatag();
        //2.spy
        busIcsRtnDat =spy(BusIcsRtnDat.builder().inputDto(this.inputDto).build());

        // when
        String res = busIcsRtnDat.validataParamsDatag();
        busIcsRtnDat.validataParamsDatag();
        // then
        verify(busIcsRtnDat,times(2)).validataParamsDatag();
        assertThat(res).isEqualToIgnoringCase(VAILATE_PASS_MSG);
    }

}

场景三:验证捕获参数

 
    @Test
    public void give服务层一个Dao对象_when使用参数调用服务更新方法_then验证更新方法的入参值() {
        //give
        PersonDao personDao = mock(PersonDao.class);
        PersonService personService = new PersonService(personDao);

        //when
        ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
        personService.update(1, "jack");

        //then
        verify(personDao).update(argument.capture());
        assertEquals(1, argument.getValue().getId());
        assertEquals("jack", argument.getValue().getName());
    }

    class Person {
        private int id;
        private String name;

        Person(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }

    interface PersonDao {
        public void update(Person person);
    }

    class PersonService {
        private PersonDao personDao;

        PersonService(PersonDao personDao) {
            this.personDao = personDao;
        }

        public void update(int id, String name) {
            personDao.update(new Person(id, name));
        }
    }
}   

场景四:验证执行顺序

public void give定义三个执行对象_when调用方法_then验证方法的执行顺序(){
    //give
    List list = mock(List.class);
    List list2 = mock(List.class);
    list.add(1);
    list2.add("hello");
    list.add(2);
    list2.add("world");

    //when
    //将需要排序的mock对象放入InOrder
    InOrder inOrder = inOrder(list,list2);

    //then
    //下面的代码不能颠倒顺序,验证执行顺序
    inOrder.verify(list).add(1);
    inOrder.verify(list2).add("hello");
    inOrder.verify(list).add(2);
    inOrder.verify(list2).add("world");
} 

场景五:注解方式引入Mock及Spy对象

class CashRegisterLegacyTest {

   @Mock private Printer mockPrinter;

   @Mock private Purchase stubPurchase;

   @Spy private CashRegisterLegacy spyCashRegister;

   @Test
   void give定义完调用的返回值_when进行打印小票信息_then能获得前面定义的小票信息 {
       //Given
       final String DESCRIPTION = "Purchase description";
       doReturn(DESCRIPTION).when(stubPurchase).description();
       doReturn(10).when(spyCashRegister).calculateDiscount(stubPurchase);
       doReturn(mockPrinter).when(spyCashRegister).buildPrinter();

       //When
       spyCashRegister.process(stubPurchase);

       //Then
       verify(mockPrinter).print(DESCRIPTION + "\ndiscount: " + 10);
       verify(spyCashRegister).calculateDiscount(stubPurchase);
   }
}

二、优良单元测试代码收集

每日代码检视中大家评审认为比较好的代码,请粘贴到这里;

@xxx @xxx @xxx @xx@xx @xx @xxx @xxx @xxx

上一篇 下一篇

猜你喜欢

热点阅读