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 20:20:45 浏览: 27
这段代码是一个JUnit测试类,用于测试SmartmallCommentService类中的query方法。这个方法用于根据不同的参数查询商品评论,并返回一个评论列表。在测试中,使用了JUnit的断言方法来判断查询结果是否符合预期。setUp方法用于在测试开始前初始化测试环境,tearDown方法用于在测试结束后清理测试环境。在测试类中使用了SpringBoot的测试注解@SpringBootTest和@RunWith(SpringRunner.class)来启用Spring容器,@Transactional注解用于将测试方法放在一个事务中,以保证测试数据不污染数据库。在query方法中,先定义了一些查询参数,然后根据showType的值来判断查询类型,如果showType为0或1,则执行查询并判断查询结果是否符合预期,如果showType不为0或1,则抛出一个运行时异常。
相关问题
import com.github.pagehelper.PageInfo;
`import com.github.pagehelper.PageInfo;`是Java代码中的一个导入语句,用于引入`com.github.pagehelper.PageInfo`类。通过这个导入语句,我们可以在当前的Java文件中使用`PageInfo`类的功能和方法。
范例:
```java
import com.github.pagehelper.PageInfo;
public class Example {
public static void main(String[] args) {
// 创建一个PageInfo对象
PageInfo<String> pageInfo = new PageInfo<>();
// 使用PageInfo对象的方法
pageInfo.setPageSize(10);
pageInfo.setTotal(100);
pageInfo.setList(Arrays.asList("A", "B", "C"));
// 打印PageInfo对象的信息
System.out.println("Page size: " + pageInfo.getPageSize());
System.out.println("Total count: " + pageInfo.getTotal());
System.out.println("Data list: " + pageInfo.getList());
}
}
```
这个范例演示了如何使用`import com.github.pagehelper.PageInfo;`导入语句,并创建一个`PageInfo`对象,并使用其方法设置和获取对象的属性。最后,打印了对象的信息。
import com.github.pagehelper.PageInfo;报错
这个报错可能是因为你的项目中没有引入相应的依赖。你可以检查一下你的项目中是否已经引入了 PageHelper 的相关依赖,以及是否正确配置了依赖版本号等信息。
如果你是使用 Maven 进行项目管理的话,可以在 pom.xml 文件中加入以下依赖:
```
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>x.x.x</version>
</dependency>
```
注意将其中的版本号替换为你需要使用的版本号。如果你使用的是 Gradle 进行项目管理,可以在 build.gradle 文件中加入以下依赖:
```
compile group: 'com.github.pagehelper', name: 'pagehelper', version: 'x.x.x'
```
同样,将其中的版本号替换为你需要使用的版本号。添加依赖后,记得重新构建你的项目,以便让依赖生效。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gitignore](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)