码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot 单元测试

时间:2020-10-09 20:39:22      阅读:20      评论:0      收藏:0      [点我收藏+]

标签:assign   lis   使用   center   public   conf   pex   要求   抛出异常   

1.pom.xml

一般使用idea新建一个SpringBoot web项目时,一般都会自动引入此依赖,如果没有,请手动引入。

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

2.测试类基类

技术图片

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ExamCenterApplication.class)
@WebAppConfiguration
public class TmallApplicationTests {

    @Before
    public void init() {
        log.info("开始测试-----------------");
    }

    @After
    public void after() {
        log.info("测试结束-----------------");
    }
}

3.测试类

我这里建一个测试类,继承上面的基类TmallApplicationTests,然后测试service中的方法

@Slf4j
public class PublishRecordTest extends TmallApplicationTests {

    @Autowired
    private ExamPublishRecordService examPublishRecordService;

    @Test
    public void testGetPublishRecord() {
        ExamPublishRecordQuery queryDTO = new ExamPublishRecordQuery();
        queryDTO.setCompanyId(1303608177161404416L);
        Assert.assertNotNull("获取成功",examPublishRecordService.getPublishRecord(queryDTO));
    }

    @Test
    public void testGetPublishRecordById() {
        Assert.assertNotNull("获取成功",examPublishRecordService.getPublishRecordById(1309026644677099520L));
    }

    @Test
    public void testGetAllReviewer() {
        Assert.assertNotNull("获取成功",examPublishRecordService.getAllReviewer());
    }

    @Test
    public void testGetAllPaper() {
        Assert.assertNotNull("获取成功",examPublishRecordService.getAllPaper(0L));
    }

    @Test
    public void testAddExam() {
        ExamAddDTO examAddDTO = new ExamAddDTO();
        examAddDTO.setTitle("单元测试标题");
        examAddDTO.setPublisher(1310123221122547712L);
        examAddDTO.setCompanyId(1303686921804840960L);
        examAddDTO.setPaperId(1309036090279067648L);
        List<Long> reviewerId = new ArrayList<>();
        reviewerId.add(1310123221122547712L);
        examAddDTO.setReviewerId(reviewerId);
        examAddDTO.setMarkingMode(0L);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            examAddDTO.setMarkingStopTime(formatter.parse("2020-10-24 12:00:00"));
            examAddDTO.setStartTime(formatter.parse("2020-10-01 12:00:00"));
            examAddDTO.setEndTime(formatter.parse("2020-10-02 12:00:00"));
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        examAddDTO.setLimitTime(120);
        examAddDTO.setPlanPeopleNum(100);
        Assert.assertEquals(true,examPublishRecordService.addExam(examAddDTO));
    }

    @Test
    public void testPublishExam() {
        List<PublishExamDTO> publishExamDTOS = new ArrayList<>();
        PublishExamDTO publishExamDTO = new PublishExamDTO();
        publishExamDTO.setId(1309379658076127232L);
        publishExamDTO.setVersion(2L);
        publishExamDTOS.add(publishExamDTO);
        Assert.assertEquals(1,examPublishRecordService.publishExam(publishExamDTOS));
    }

    @Test
    public void testDeletePublishRecord() {
        List<ExamPublishRecordDeleteDTO> examPublishRecordDeleteDTOS = new ArrayList<>();
        ExamPublishRecordDeleteDTO examPublishRecordDeleteDTO = new ExamPublishRecordDeleteDTO();
        examPublishRecordDeleteDTO.setId(1310855778705342464L);
        examPublishRecordDeleteDTO.setVersion(0L);
        examPublishRecordDeleteDTOS.add(examPublishRecordDeleteDTO);
        Assert.assertEquals(1,examPublishRecordService.deletePublishRecord(examPublishRecordDeleteDTOS));
    }

    @Test
    public void testUpdateExam() {
        ExamUpdateDTO examUpdateDTO = new ExamUpdateDTO();
        examUpdateDTO.setId(1310948165364482048L);
        examUpdateDTO.setTitle("单元测试修改标题");
        examUpdateDTO.setPaperId(1309036340158922752L);
        List<Long> reviewerId = new ArrayList<>();
        reviewerId.add(1310123221122547712L);
        examUpdateDTO.setReviewerId(reviewerId);
        examUpdateDTO.setMarkingMode(0L);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            examUpdateDTO.setMarkingStopTime(formatter.parse("2020-10-24 12:00:00"));
            examUpdateDTO.setStartTime(formatter.parse("2020-10-01 12:00:00"));
            examUpdateDTO.setEndTime(formatter.parse("2020-10-02 12:00:00"));
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        examUpdateDTO.setLimitTime(120);
        examUpdateDTO.setPlanPeopleNum(100);
        examUpdateDTO.setVersion(1L);
        examUpdateDTO.setStatus(Byte.valueOf("0"));
        Assert.assertEquals(1,examPublishRecordService.updateExam(examUpdateDTO));
    }

    @Test
    public void testStopExam() {
        List<PublishExamDTO> publishExamDTOS = new ArrayList<>();
        PublishExamDTO publishExamDTO = new PublishExamDTO();
        publishExamDTO.setId(1309379658076127232L);
        publishExamDTO.setVersion(3L);
        publishExamDTOS.add(publishExamDTO);
        Assert.assertEquals(1,examPublishRecordService.stopExam(publishExamDTOS));
    }

    @Test
    public void testSetReviewTimeOut() {
        List<PublishExamDTO> publishExamDTOS = new ArrayList<>();
        PublishExamDTO publishExamDTO = new PublishExamDTO();
        publishExamDTO.setId(1309379658076127232L);
        publishExamDTO.setVersion(5L);
        publishExamDTOS.add(publishExamDTO);
        assertEquals(1,examPublishRecordService.setReviewTimeOut(publishExamDTOS));
    }

    @Test
    public void testGetPaperDetail() {
        Long paperId = 1309035992727945216L;
        assertNotNull(examPublishRecordService.getPaperDetail(paperId));
    }
}

如上,直接@Autowired引入你想测试的类就好,然后继承基类,测试方法上面要加@Test注解。

其中,比较重要的是Assert断言的使用

总结如下:

  1. notNull(Object object)
      当 object 不为 null 时抛出异常,notNull(Object object, String message) 方法允许您通过 message 定制异常信息。和 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null;

  2. isTrue(boolean expression) / isTrue(boolean expression, String message)
      当 expression 不为 true 抛出异常;

  3. notEmpty(Collection collection) / notEmpty(Collection collection, String message)
      当集合未包含元素时抛出异常。
      notEmpty(Map map) / notEmpty(Map map, String message) 和 notEmpty(Object[] array, String message) / notEmpty(Object[] array, String message) 分别对 Map 和 Object[] 类型的入参进行判断;

  4. hasLength(String text) / hasLength(String text, String message)
      当 text 为 null 或长度为 0 时抛出异常;

  5. hasText(String text) / hasText(String text, String message)
      text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;

  6. isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message)
      如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;

  7. isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message)
      subType 必须可以按类型匹配于 superType,否则将抛出异常;

参考:spring boot写单元测试(测试service层)

spring boot 单元测试

标签:assign   lis   使用   center   public   conf   pex   要求   抛出异常   

原文地址:https://www.cnblogs.com/spike218/p/13767438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!