千图网素材免费,西安网站优化,html代码编辑器,网页开发与网站开发Junit与Spring Test简单使用 Junit5简介Junit5 注解Junit5与Spring结合 差异概览MockingMockBeanSpyBeanDemo 注意事项 又要写测试代码了#xff0c;总结记录一下。 Junit5简介 与单一模块设计的Junit4不同,Junit5引入了模块化架构,由三个主要子项目组成#xff1a; JUnit Pl… Junit与Spring Test简单使用 Junit5简介Junit5 注解Junit5与Spring结合 差异概览MockingMockBeanSpyBeanDemo 注意事项 又要写测试代码了总结记录一下。 Junit5简介 与单一模块设计的Junit4不同,Junit5引入了模块化架构,由三个主要子项目组成 JUnit Platform测试运行的基础平台支持不同的测试引擎不仅仅是 JUnit还可以扩展其他测试框架如 TestNG。JUnit JupiterJUnit5 的新编程模型和扩展模型包含新的注解和测试方法如 Test, BeforeEachAfterEach 等。JUnit Vintage提供对 JUnit4 及更早版本的向后兼容支持因此 JUnit5 可以运行旧的 JUnit4 测试代码。 Junit5 注解
BeforeEach 和 AfterEach取代了 JUnit4 的 Before 和 After作用于每个测试方法。BeforeAll 和 AfterAll取代了 JUnit4 的 BeforeClass 和 AfterClass可以作用于整个类生命周期且在 JUnit5 中可以是非静态方法通过注入 TestInstance。DisplayName允许为测试方法和类指定自定义名称方便生成更具可读性的测试报告。Nested支持嵌套的测试类便于组织复杂的测试场景。ParameterizedTest增强了参数化测试的支持允许为测试方法传递多个参数集。DynamicTest动态创建测试用例支持灵活的测试流程。 BeforeAllAfterAll 类级别只执行一次 BeforeEach AfterEach 方法级别每个方法都会执行 All 和 each的区别在执行类级别测试时才能看出来: all仅执行一次,each执行次数取决于有多少个Test方法 Junit5与Spring结合
在与Spring集成时,不再使用RunWithExtendWith, 指定拓展为Spring, 测试中可以使用Spring注解进行依赖注入,ContextConfiguration指定配置类Transactional, 测试中提供事务支持Spring框架提供的 MockBean SpyBean注解, 提供Mocking支持,模拟Bean行为 在springboot项目中SpringBootTest注解内部就是使用了 ExtendWith({SpringExtension.class})提前帮我们配置好了 差异概览
功能/特性JUnit4JUnit5Spring Test架构单一模块模块化架构Platform, Jupiter, Vintage基于 TestContext 框架与 JUnit 集成注解Test, Before, AfterTest, BeforeEach, AfterEachContextConfiguration, Transactional扩展机制RunWith, TestRuleExtendWith, TestInstanceExtendWith(SpringExtension.class)参数化测试较弱的参数化测试支持强大的参数化测试支持与 Spring 环境集成支持 Mock 和依赖注入事务管理N/AN/A支持 Transactional测试完成后自动回滚Spring 集成RunWith(SpringJUnit4ClassRunner.class)ExtendWith(SpringExtension.class)内置的对 Spring 上下文的管理和 Bean 注入支持
Mocking
MockBean
如果不指定规则则mockBean执行完返回默认值即对象为null数字为0
如果指定了规则就按照规则返回下面例子按照规则返回ok
SpyBean
有规则按照规则走没有规则按照真实服务进行。比如在多服务调用过程中如果部分服务不可用可以定义规则如果服务可用则调用真实的服务。
Demo
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doReturn;SpringBootTest
class DemoTestApplicationTests {SpyBeanMyService myService;// 所有方法使用myService方法,可自定义覆盖MockBeanMyService2 myService2; // 所有方法需要自定义返回,否则使用默认初始值或null// 调用Spy模拟服务,Testvoid invokeSpy(){// 仅定义hello的mock规则doReturn(hello handsome).when(myService).hello(wyy);assertEquals(hello handsome, myService.hello(wyy));// 未定义规则,直接调用say()逻辑assertEquals(hello, myService.say(hello));}// 调用Mock模拟服务,Testvoid invokeMock(){// 仅定义hello的mock规则doReturn(hello handsome).when(myService2).hello(wyy);assertEquals(hello handsome, myService2.hello(wyy));// 未定义规则,返回初始值或nullassertEquals(hello, myService2.say(hello));}// All 和 each的区别,执行单独的测试方法区分不了,// 在执行类级别测试时才能看出来: all仅执行一次,each执行次数取决于有多少个Test方法BeforeAllstatic void beforeAll(){System.out.println(before all);}BeforeEachvoid beforeEach(){System.out.println(before each);}AfterEachvoid afterEach(){System.out.println(after each);}AfterAllstatic void afterAll(){System.out.println(after all);}
}
注意事项
使用断言进行判断严禁System.out进行人工判断丰富测试场景的多样性通过不同参数测试增加多样性提升测试覆盖率