JUnit4-Runner

本文深入解析了JUnit4中各类Runner的功能与使用方法,包括默认的BlockJUnit4ClassRunner,用于批量运行测试类的SuiteRunner,适合参数化测试的ParameterizedRunner,以及CategoriesRunner和EnclosedRunner等特殊用途的Runner。

Catalog

BlockJUnit4ClassRunner  

Suite Runner

Parameterized Runner

Categories Runner

Enclosed Runner

 

BlockJUnit4ClassRunner  

 BlockJUnite4ClassRunner ,是JUnit4默认的Runner。

在测试类上添加@RunWith(BlockJUnit4ClassRunner.class),效果和不添加一样。

 

 

Suite Runner

 Suite.class  ,在项目中,只写一个测试类是不可能的,我们会写很多很多的测试类,当要一起依次运行这些测试类时,可

以使用JUnit为我们提供的打包功能,将所有需要运行的测试类集中起来,一次性运行完毕。

To use it, annotate a class with @RunWith(Suite.class) and @SuiteClasses({TestClass1.class, ...}).

 When you run this class, it will run all the tests in all the suite classes. 

总结:

  在JUnit4中,如果想要同时运行多个测试类,需要使用两个注解:

  @RunWith(Suite.class)指定使用Suite运行器来运行测试;

  @SuiteClasses(ClassName.class)指定运行哪些测试类。测试类可以指定为Suite,这样JUnit会继续再去寻找里面的

测试类,一直找到能够执行的Test Case并执行之。

 1 //指定运行器
 2 @RunWith(Suite.class)
 3 //指定要测试的类
 4 @Suite.SuiteClasses({
 5     MathDemoTest.class,
 6     GradeDemoTest.class
 7 })
 8 public class TestTwoDemo {
 9 
10 }

 

运行结果图(程序中有个故意失败的测试用例):

 

 PS:在一个套件中也可以包含另一个套件类,写法上注意没有大扩折号{} 

1 @RunWith(Suite.class)
2 @SuiteClasses(
3     TestTwoDemo.class
4 )
5 public class TestAll {
6 
7 }

 

Parameterized Runner

Parameterized.class 是根据所设计的参数来执行测试。常用于参数化测试。

1.@RunWith(Parameterized.class) 指定使用arameterized运行器来运行测试;

2.@Parameters注解置于提供数据的静态static方法上,并且返回一个集合Collection。

 

 

 

 

总结:

其他特殊的Runner有:

  1. Suite Runner 实现打包测试
  2. Parameterized.class
  3. Categories.class
  4. Enclosed.class

转载于:https://www.cnblogs.com/Mokaffe/p/4963197.html

在使用 `SpringJUnit4ClassRunner` 进行单元测试时,如果出现报红(错误或警告),通常是由于依赖缺失、版本不兼容或配置问题所致。以下是常见的问题原因及解决方案。 ### 1. 缺少 `spring-test` 依赖 最常见的原因是项目中未正确引入 `spring-test` 模块。`SpringJUnit4ClassRunner` 是 Spring 框架提供的测试支持类,必须依赖 `spring-test` 包。请确保在 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.20</version> <!-- 请根据实际Spring版本选择 --> </dependency> ``` 若项目中使用较旧的 Spring 版本(如 4.x 或 5.0.x),请相应调整版本号[^4]。 ### 2. 依赖作用域配置错误 在某些情况下,`spring-test` 的依赖可能被错误地配置了 `<scope>` 标签,例如设置为 `provided` 或 `test` 以外的值,这会导致测试运行时类路径中找不到相关类。应确保依赖配置如下: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.20</version> <scope>test</scope> </dependency> ``` 若 `<scope>` 被设置为 `compile` 或其他值,可能会导致构建过程中依赖处理异常[^3]。 ### 3. JUnit 版本不兼容 `SpringJUnit4ClassRunner` 是为 JUnit 4 设计的测试运行器,若项目使用的是 JUnit 5(即 `org.junit.jupiter` 包),则会导致 `ClassNotFoundException` 或注解无法识别。请确认是否使用了正确的 JUnit 版本: - 若使用 JUnit 4,请确保引入以下依赖: ```xml <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> ``` - 若使用 JUnit 5,则应改用 `SpringRunner`(适用于 Spring Boot 2.x)或 `SpringExtension`: ```java import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @ContextConfiguration({"/spring/spring.xml"}) public class MyTestClass { // 测试代码 } ``` ### 4. IDE 缓存或构建问题 有时 IDE(如 IntelliJ IDEA 或 Eclipse)未正确刷新依赖或构建测试类路径,导致测试类无法识别。可尝试以下操作: - 执行 Maven 清理与重新导入依赖: ```bash mvn clean install ``` - 在 IDE 中刷新 Maven 项目(如点击 "Reimport")。 - 清理并重建项目,确保测试类路径包含 `spring-test` 和 `junit` 相关的 JAR 包。 ### 5. 检查类路径与导入语句 确保测试类中正确导入了以下类: ```java import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ``` 若导入语句错误(例如使用了 `org.springframework.test.context.junit.jupiter.SpringExtension` 而非 JUnit 5 的注解),也可能导致编译或运行时错误。 ### 示例测试类 以下是一个使用 `SpringJUnit4ClassRunner` 的完整测试类示例: ```java import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/spring/spring.xml"}) public class MyServiceTest { @Autowired private MyService myService; @Test public void testMyServiceMethod() { // 调用服务方法并验证结果 String result = myService.doSomething(); assert result != null; } } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值