Spring Boot配置Junit(单元测试)
1 Junit
JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。 JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。
2 Maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
3 TestService
package com.service; import org.springframework.stereotype.Service; @Service public class TestService { public void printString(String str) { System.out.println(str); } }
4 UnitTest
测试代码,存在于test目录下。
package com.test; import com.DemoApplication; import com.service.TestService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest(classes = DemoApplication.class) public class UnitTest { @Autowired private TestService testService; @Test public void test(){ testService.printString("123456"); } }
5 测试结果
上一篇:
5款热门的远程控制软件,让你事半功倍