@RunWith 的意思
时间: 2023-07-24 21:57:30 浏览: 93
@RunWith 是 JUnit 框架中的一个注解,用于指定测试运行器(test runner)的类。测试运行器是 JUnit 的核心组件之一,用于控制和执行测试用例。通过使用 @RunWith 注解,可以自定义测试运行器,并提供额外的功能或行为。
通常情况下,JUnit 使用默认的测试运行器 org.junit.runners.BlockJUnit4ClassRunner 来运行测试用例。但是,通过 @RunWith 注解,可以指定其他的测试运行器,例如使用 Spring TestContext Framework 进行集成测试时,可以使用 org.springframework.test.context.junit4.SpringJUnit4ClassRunner 运行测试用例。
使用 @RunWith 注解时,需要在测试类上加上该注解,并将需要使用的测试运行器类作为参数传入注解中。例如:
```java
@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest {
// 测试方法
}
```
这样,在执行 MyTest 类中的测试方法时,将使用 SpringJUnit4ClassRunner 运行器来执行测试。
相关问题
@RunWith(SpringRunner.class) 是什么意思
@RunWith(SpringRunner.class) 是 JUnit4 的注解之一,它指定了在运行测试时所使用的运行器(Runner),而 @SpringBootTest 注解则是用于 Spring Boot 应用程序的集成测试上,它会启动完整的应用程序上下文,并提供一个模拟的 Web 环境。这两个注解通常一起使用,以确保在测试中使用 Spring Boot 上下文和相关的 Spring 功能。
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); } } }中各代码的意思
这段代码是一个JUnit测试类,用于测试SmartmallCommentService类中的query方法。这个方法用于根据不同的参数查询商品评论,并返回一个评论列表。在测试中,使用了JUnit的断言方法来判断查询结果是否符合预期。setUp方法用于在测试开始前初始化测试环境,tearDown方法用于在测试结束后清理测试环境。在测试类中使用了SpringBoot的测试注解@SpringBootTest和@RunWith(SpringRunner.class)来启用Spring容器,@Transactional注解用于将测试方法放在一个事务中,以保证测试数据不污染数据库。在query方法中,先定义了一些查询参数,然后根据showType的值来判断查询类型,如果showType为0或1,则执行查询并判断查询结果是否符合预期,如果showType不为0或1,则抛出一个运行时异常。
阅读全文
相关推荐

















