JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量。
JUnit 测试框架具有以下重要特性:
测试工具
测试套件
测试运行器
测试分类
SpringBoot中引入junit,pom.xml中添加
<!-- ______________________________________________ junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
这里定义一个测试的基类,用于初始化和引入公用的模块。
这里初始化了一个操作Redis的接口类和测试SpringMVC的MockMvc类。
package com.example.demo; import org.junit.jupiter.api.BeforeEach; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.example.demo.redis.RedisDao; /** * MVC测试基类 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = DemoApplication.class) public abstract class TestSupport { /** * 模拟mvc测试对象 */ protected MockMvc mvc; @Autowired protected RedisDao redisDao; /** * web项目上下文 */ @Autowired private WebApplicationContext webApplicationContext; /** * 所有测试方法执行之前执行该方法 */ @BeforeEach public void before() throws Exception { mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } public MockHttpServletRequestBuilder post(String uri){ return MockMvcRequestBuilders.post(uri).accept(MediaType.APPLICATION_JSON); } public MockHttpServletRequestBuilder get(String uri){ return MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON); } // @RunWith:标识为JUnit的运行环境; // @SpringBootTest:获取启动类、加载配置,确定装载Spring Boot; // @Test:声明需要测试的方法; // @BeforeClass:针对所有测试,只执行一次,且必须为static void; // @AfterClass:针对所有测试,只执行一次,且必须为static void; // @Before:每个测试方法前都会执行的方法;新版本为@BeforeEach // @After:每个测试方法前都会执行的方法;新版本为@ AfterEach // @Ignore:忽略方法; // Assert.assertEquals 对比两个值相等 // Assert.assertNotEquals 对比两个值不相等 // Assert.assertSame 对比两个对象的引用相等 // Assert.assertArrayEquals 对比两个数组相等 // Assert.assertTrue 验证返回是否为真 // Assert.assertFlase 验证返回是否为假 // Assert.assertNull 验证null // Assert.assertNotNull 验证非null }
一个单元测试类执行顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:
@Before –> @Test –> @After
这里说一个坑,如果用低版本的junit时,使用的是@Before,但是你会发现现在不起作用了,那是因为新版本用@BeforeEach替换了,而且你使用也没有错误提示,只是不走初始化方法。
官方说明:
然后我们编写实际需要测试的接口处理类,这里测试了两个一个是Redis的使用一个是SpringMVC的调用。
package com.example.demo; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.RequestBuilder; /** * MVC测试 */ class DemoApplicationTests extends TestSupport { @Test public void testRedis() throws Exception{ redisDao.setKey("name","javacui"); redisDao.setKey("age","35"); System.out.println(redisDao.getValue("name")); System.out.println(redisDao.getValue("age")); } @Test public void testMVC01() throws Exception { // 传递的参数为JSON,这里不传递参数 RequestBuilder request = post("/hello").contentType(MediaType.APPLICATION_JSON).content(""); MvcResult mvcResult = mvc.perform(request).andReturn(); int status = mvcResult.getResponse().getStatus(); System.out.println("status is: " + status); Assert.assertTrue("错误,正确的返回值为200", status == 200); System.out.println(mvcResult.getResponse().getContentAsString()); } }
我特别讨厌网上发代码不发import的人。
配图
END