当前位置: 首页 > news >正文

文章网建站男女性直接做的视频网站

文章网建站,男女性直接做的视频网站,什么是电子商务网站建设,中小企业erp系统哪个好JUnit测试运行器(Test Runner)决定了JUnit如何执行测试。JUnit有多个测试运行器,每个运行器都有特定的功能和用途。 1. 默认运行器 当没有显式指定运行器时,JUnit会使用默认运行器,这在JUnit 4和JUnit 5之间有所不同…

JUnit测试运行器(Test Runner)决定了JUnit如何执行测试。JUnit有多个测试运行器,每个运行器都有特定的功能和用途。

1. 默认运行器

当没有显式指定运行器时,JUnit会使用默认运行器,这在JUnit 4和JUnit 5之间有所不同。

JUnit 4 默认运行器

在JUnit 4中,默认运行器是BlockJUnit4ClassRunner

import org.junit.Test;
import static org.junit.Assert.assertEquals;public class DefaultRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
JUnit 5 默认运行器

在JUnit 5中,不需要显式指定运行器,JUnit Jupiter会自动运行测试。

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;public class DefaultRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}

2. @RunWith 注解

@RunWith注解用于指定JUnit 4的测试运行器。以下是一些常用的运行器:

2.1 SpringRunner

SpringRunner(原名SpringJUnit4ClassRunner)用于在JUnit 4中运行Spring测试,提供Spring应用程序上下文的支持。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class)
public class SpringRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
2.2 Parameterized

Parameterized运行器用于运行参数化测试,即同一个测试方法可以用不同的参数多次运行。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;@RunWith(Parameterized.class)
public class ParameterizedTest {private int input1;private int input2;private int expected;public ParameterizedTest(int input1, int input2, int expected) {this.input1 = input1;this.input2 = input2;this.expected = expected;}@Parameterized.Parameterspublic static Collection<Object[]> data() {return Arrays.asList(new Object[][] {{ 1, 2, 3 },{ 2, 3, 5 },{ 3, 5, 8 }});}@Testpublic void testAdd() {assertEquals(expected, input1 + input2);}
}
2.3 Suite

Suite运行器用于运行一组测试类。

import org.junit.runner.RunWith;
import org.junit.runners.Suite;@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class,TestClass2.class
})
public class TestSuite {// 空类,仅用于运行指定的测试类
}

3. JUnit 5 扩展模型

在JUnit 5中,不使用@RunWith,而是使用@ExtendWith注解来扩展测试功能。

3.1 SpringExtension

SpringExtension用于在JUnit 5中运行Spring测试,提供Spring应用程序上下文的支持。

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;@ExtendWith(SpringExtension.class)
public class SpringExtensionTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
3.2 ParameterizedTest

@ParameterizedTest注解用于运行参数化测试,即同一个测试方法可以用不同的参数多次运行。

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;public class ParameterizedTestExample {@ParameterizedTest@CsvSource({"1, 2, 3","2, 3, 5","3, 5, 8"})void testAdd(int input1, int input2, int expected) {assertEquals(expected, input1 + input2);}
}

4. 自定义运行器和扩展

4.1 自定义JUnit 4 运行器

可以创建自定义运行器来扩展JUnit 4的功能。

import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;public class CustomRunner extends BlockJUnit4ClassRunner {public CustomRunner(Class<?> klass) throws InitializationError {super(klass);}@Overrideprotected void runChild(org.junit.runners.model.FrameworkMethod method, org.junit.runner.notification.RunNotifier notifier) {System.out.println("Running test: " + method.getName());super.runChild(method, notifier);}
}import org.junit.Test;
import org.junit.runner.RunWith;@RunWith(CustomRunner.class)
public class CustomRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
4.2 自定义JUnit 5 扩展

可以创建自定义扩展来扩展JUnit 5的功能。

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;public class CustomExtension implements BeforeEachCallback {@Overridepublic void beforeEach(ExtensionContext context) {System.out.println("Before each test: " + context.getDisplayName());}
}import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;@ExtendWith(CustomExtension.class)
public class CustomExtensionTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}

5. 使用示例:Spring Boot 测试

结合使用@ExtendWith@SpringBootTest进行Spring Boot应用程序的集成测试。

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MyApplicationTests {@Autowiredprivate MyService myService;@Testpublic void testAdd() {assertEquals(5, myService.add(2, 3));}
}

总结

  • JUnit 4中,@RunWith用于指定测试运行器,常用的运行器包括SpringRunnerParameterizedSuite等。
  • JUnit 5中,@ExtendWith用于扩展测试功能,常用的扩展包括SpringExtensionParameterizedTest等。
  • 可以创建自定义运行器(JUnit 4)或扩展(JUnit 5)来满足特定测试需求。
  • @SpringBootTest用于Spring Boot应用程序的集成测试。

这些工具和技术使得JUnit能够灵活地适应各种测试需求。

http://www.yayakq.cn/news/988295/

相关文章:

  • 网站制作价格推 荐网站建设的空间是什么
  • 衡水网站制作报价东莞网络营销外包有哪些
  • 会python做网站腾讯公众号小程序
  • 西安建设网站首页上海比较好的公司排名
  • 做网站怎样找如何创立自己的网址
  • 怎样用盒子做汽车视频网站电商网站 建社区
  • 厦门网络公司网站开发计算机网络技术就业方向工资
  • 个人网站的网页安徽网站建设方案优化
  • 如何做资讯网站网站子站怎么建设
  • 山东天成水利建设 网站安徽工程建设信息网实名制查询
  • 池州网站建设怎么样物流单号查询网站建设
  • 做营销型网站一般要多少钱色轮配色网站
  • 建设常规的网站报价是多少成都工装公司
  • 定制网站的优势网上推广服务
  • 用dw做网站结构图国内优秀网站
  • 网站首页添加标签soe标题打开直接显示网站怎么做
  • 博罗网站建设出口网站制作
  • 有关网站建设的合同沈阳公司建站
  • 做响应式网站的体会互联网公司排名保定
  • 公司申请网站需要哪些材料网站开发体系
  • 建网站需要注册公司吗编程是什么东西
  • asp网站新闻置顶wordpress 建立数据库连接时出错 用户名密码可能不正确
  • 做了微网站sem优化方法
  • 南京网站设计我选柚米科技拉销智能模板建站系统
  • 做微商城网站免费做网站的公司
  • 英文公司网站图片发到哪些网站 seo
  • 做彩票平台网站吗沈阳建设工程交易中心官网
  • 深圳建设局网站宝安分中心wordpress图片加链接地址
  • 室内设计欣赏网站500人企业的网络搭建
  • 建设银行网站能变更手机号吗东莞服务公司网站建设