package com.sgave.mall.db.service; import com.github.pagehelper.PageInfo; import com.sgave.mall.db.domain.SmartmallComment; import org.junit.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestContextManager; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Arrays; import java.util.Collection; import java.util.List; import static org.junit.Assert.*; @SpringBootTest @RunWith(SpringRunner.class) /*@RunWith(Parameterized.class)*/ @Transactional public class SmartmallCommentServiceTest { @Autowired private SmartmallCommentService scs; @Before public void setUp() throws Exception { TestContextManager testContextManager = new TestContextManager(getClass()); testContextManager.prepareTestInstance(this); SmartmallCommentService scs = new SmartmallCommentService(); } @After public void tearDown() throws Exception { scs=null; } @Test public void query() { Byte type = (byte)0; Integer valueId = 9008001; Integer showType = 2; Integer offset = 0; Integer limit = 1; /*List<SmartmallComment> comments = scs.query(0,9008001,0,0,5);*/ /*List<SmartmallComment> comments = scs.query(1,9008002,1,0,5);*/ /*List<SmartmallComment> comments = scs.query(1,9008001,3,0,5);*/ if (showType == 0 || showType == 1) { List<SmartmallComment> comments = scs.query(type,valueId,showType,offset,limit); long act=PageInfo.of(comments).getTotal(); if (showType == 0) { long exp = 2; assertEquals(exp,act); } else if (showType == 1) { long exp = 1; assertEquals(exp,act); } }else { String exp="showType不支持"; String act = assertThrows(RuntimeException.class,() ->scs.query(type,valueId,showType,offset,limit)).getMessage() ; assertEquals(exp,act); } } }中各代码的意思
时间: 2024-04-26 17:20:45 浏览: 194
这段代码是一个JUnit测试类,用于测试SmartmallCommentService类中的query方法。这个方法用于根据不同的参数查询商品评论,并返回一个评论列表。在测试中,使用了JUnit的断言方法来判断查询结果是否符合预期。setUp方法用于在测试开始前初始化测试环境,tearDown方法用于在测试结束后清理测试环境。在测试类中使用了SpringBoot的测试注解@SpringBootTest和@RunWith(SpringRunner.class)来启用Spring容器,@Transactional注解用于将测试方法放在一个事务中,以保证测试数据不污染数据库。在query方法中,先定义了一些查询参数,然后根据showType的值来判断查询类型,如果showType为0或1,则执行查询并判断查询结果是否符合预期,如果showType不为0或1,则抛出一个运行时异常。
阅读全文