junit runwith

article/2025/9/18 12:10:14

junit的runwith是给单元测试中增加一些附属特性,今天在看flink源码的时候,发现其使用了一个注解@RunWith(Parameterized.class),有点好奇,于是研究了一下这个注解的使用。
这个注解产生的背景是这样的:flink中的每个功能要保证在单机模式和集群模式下都能使用,因此如果编写常规的junit test函数,对于每个要测试的功能A,我们需要写两个测试函数AOnSingleMode()AOnClusterMode,这无疑是一种折磨。因此,我们只需要使用注解@RunWith(Parameterized.class),然后在参数@Parameterized.Parameters()定义两种模式:单机模式和集群模式,junit就会默认使用这两种参数去初始化测试类进行测试(因此在测试类中需要一个接收参数的构造函数)。

下面给个实例

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;import java.util.Arrays;
import java.util.Collection;@RunWith(Parameterized.class)
public class ParameterizedTest {public String mode;public ParameterizedTest(String mode) {this.mode = mode;}@Testpublic void testParameterized(){System.out.println(String.format("当前所属模式:%s", mode));}@Parameterized.Parameters(name = "execute mode = {0}") // 给这个测试一个名字public static Collection<String> executionModes(){return Arrays.asList("集群模式","单例模式");}
}

测试结果:
在这里插入图片描述


如上面所写,每个测试类需要自己实现public static Collection<String> executionModes()方法,有点烦,为了进一步简化,我们可以构造一个测试类的公共父类,在父类中实现这个方法,然后在每个测试类中继承父类就好了(实际上flink就使用的这种方式):

import org.junit.runners.Parameterized;import java.util.Arrays;
import java.util.Collection;// 父类
public class CommonTest {@Parameterized.Parameters()public static Collection<String> executionModes(){return Arrays.asList("集群模式","单例模式");}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;// 子类
@RunWith(Parameterized.class)
public class ParameterizedTest extends CommonTest{public String mode;public ParameterizedTest(String mode) {this.mode = mode;}@Testpublic void testParameterized(){System.out.println(String.format("当前所属模式:%s", mode));}
}

http://chatgpt.dhexx.cn/article/YfTdytyn.shtml

相关文章

springmvc--@runwith(springrunner.class)报错

目录 解决方法  1)代码 这是一个文件上传的测试类 不重要,重要的是 RunWith(SpringRunner.class)报错! import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.domain.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastF…

run()和start()方法区别

多线程原理&#xff1a;相当于玩游戏机&#xff0c;只有一个游戏机&#xff08;cpu&#xff09;&#xff0c;可是有很多人要玩&#xff0c;于是&#xff0c;start是排队&#xff01;等CPU选中你就是轮到你&#xff0c;你就run&#xff08;&#xff09;&#xff0c;当CPU的运行的…

关于@RunWith注解的一点问题

问题引入 今天在用IDEA写一个springboot测试的时候&#xff0c;碰到了一个问题。 SpringBootTest public class ServiceTest {Autowiredprivate IUserService userService;Testpublic void testSelectById(){User byId userService.getById(1);System.out.println(byId);} }…

Java学习笔记-@RunWith(SpringRunner.class)

我们在进行Test测试时&#xff0c;除了要保证测试的包和启动包是同一个路径下&#xff0c;并且需要在Test测试类上加上RunWith(SpringRunner.class)注解&#xff0c;否则的话会直接报一个NullPointerExecption的错误&#xff01; RunWith(SpringRunner.class)的作用&#xff1…

@RunWith和 SpringJUnit4ClassRunner ----junit4和Spring一起使用

今天在看Spring的Demo的时候&#xff0c;看到了如此单元测试的写法 如下: RunWIth(SpringJunit4ClassRunner.class) ContextConfiguration(locations {"classpath:applicationContext.xml"} public class MyTest { Test public void hehe() { //....... }…

@RunWith(SpringRunner.class)测试SpringRunner.class爆红

SpringRunner报红无法添加类&#xff0c;上pom.xml文件中去掉scope标签内容即可。如下图所示 然后再点击右边侧面的Reload All Maven Projects即可 方法2 如图所示&#xff0c;直接altenter强制 以上两种方法都不可用的时候&#xff0c;这个时候就得手动导入jar包了&#xf…

Java学习笔记-@RunWith(SpringRunner.class)的作用

建议阅读&#xff1a;https://blog.csdn.net/u011835956/article/details/113950577 注意&#xff1a;Test都是基于Junit写的case&#xff0c;并不是基于TestNg框架&#xff0c;如果是TestNg框架的话&#xff0c;应该要继承AbstractTestNGSpringContextTests&#xff0c;这样b…

SpringBoot在使用测试的时候是否需要@RunWith?

我们在使用SpringBoot进行测试的时候一般是需要加两个注解&#xff1a; SpringBootTest 目的是加载ApplicationContext&#xff0c;启动spring容器。 RunWith 是一个测试启动器&#xff0c;可以加载SpringBoot测试注解让测试在Spring容器环境下执行。如测试类中无此注解&#…

SpringBootTest单元测试—加入@Test无法启动测试方法,什么情况下需要加@RunWith(SpringRunner.class)?

spring环境下单元测试: SpringBoot环境下单元测试: 一、SpringBoot2.4.0之后 二、2.2.0 < SpringBoot < 2.4. 三、SpringBoot2.2.0之前 最近写SpringBootTest单元测试时&#xff0c;加入Test无法启动测试方法&#xff08;我用的是SpringBoot2.2.0之前的版本&#xff09;…

Run Run Run

学习了 2022 年集训队论文 《浅谈与 Lyndon 理论有关的字符串组合问题》 写得很好&#xff0c;像我这样的字符串小白也能看懂 Lyndon 分解 若字符串 w w w 小于它的每一个真后缀&#xff0c;则称 w w w 是 Lyndon 串。若字符串 w w w 是 Lyndon 串&#xff0c;则 w k w ′…

关于springboot在启动测试类时报错的问题和@RunWith的使用

【1】最重要的一点&#xff1a;测试类启动依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupI…

天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解

Springboot测试类之@RunWith注解 Springboot测试类之@RunWith注解Springboot测试类之@RunWith注解 @runWith注解作用: @RunWith就是一个运行器@RunWith(JUnit4.class)就是指用JUnit4来运行@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境,以便在测试开…

SSM中使用测试类RunWith

RunWith(SpringJUnit4ClassRunner.class) 需要使用两个测试包 <!--测试包--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.7.RELEASE</version></dependency>…

@RunWith的使用

文章目录 RunWith作用RunWith(SpringJunit4ClassRunner.class) 使用说明测试ActiveProfiles("dev") RunWith作用 RunWith 就是一个运行器 RunWith(JUnit4.class) 就是指用JUnit4来运行 RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境 RunWit…

使用命令行创建文件夹和创建文件

进入到指定的目录之后&#xff0c;使用命令&#xff1a;mkdir可以创建新的目录&#xff0c;使用示例&#xff1a; 创建文件&#xff0c;可以使用命令touch&#xff0c;示例&#xff1a;

cmd命令窗口如何创建和删除文件、文件夹,写入内容到文件

首先&#xff0c;同时按住键盘 winR&#xff0c;调出运行窗口&#xff0c;如下图所示&#xff1a; 接下来&#xff0c; &#xff08;1&#xff09;创建文件夹。 使用【 md 文件名 或 mkdir 文件名 】来创建。 完整命令是 md 盘符:\路径\文件夹名&#xff0c;如下图所示&#x…

如何在指定文件夹打开cmd

方法一 1.进入指定的文件夹 &#xff0c;然后选中地址栏&#xff0c;并输入cmd&#xff0c;如下图所示&#xff1a;2.回车&#xff0c;就可以打开对应路径的cmd窗口了&#xff0c;如下图所示&#xff1a; 方法二 1.进入指定的文件夹&#xff0c;按住Shift建鼠标右键&#xff0…

shell、cmd创建文件软连接

&#xff08;一&#xff09;macos shell 一、软链接创建1、创建软链接 ln -s 【目标目录】 【软链接地址】 【目标目录】指软连接指向的目标目录下&#xff0c;【软链接地址】指 “快捷键” 文件名称&#xff0c;该文件是被指令创建的 软链接创建需要同级目录下没有同名的…

windows cmd命令行新建文件,编辑文件,删除文件命令

新建文件 type nul>文件名 编辑文件 copy con 文件名 开始编辑 CTRLz回车保存 删除文件 del 文件名

cmd批量创建文件和文件夹

生成文件夹或者文件 for /l %i in (2,1,10) do md D:\test\新建文件夹%i将上述语句复制到cmd执行即可,如果是批处理要将%i改为%%i,如果是文件&#xff0c;即是 for /l %i in (2,1,10) do cd.> D:\test\新建文件%i.txt学过一点python的应该知道&#xff0c;前面是一个循环语…