1、Junit测试类使用 2、Assert断言的使用
1、项目代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "tx")
public class StartConfig {
private String test01;
private String test02;
private String test03;
private String test04;
}
2、测试类代码
- SpringRunner继承SpringJUnit4ClassRunner,使?哪?个Spring提供的测试引擎都可以。指定运?测
试的引擎
package com.lydms.securitydmo.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class StartConfigControllerTest {
@Autowired
private StartConfigController startConfig;
@Test
void get() {
System.out.println(startConfig.get());
}
}
3、Before 、@After 使用
import cn.hhxy.junit.Calculator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
@Before
public void init(){
System.out.println("init....");
}
@After
public void close(){
System.out.println("close....");
}
@Test
public void testAdd(){
System.out.println("testAdd...");
Calculator c = new Calculator();
int result = c.add(1,2);
System.out.println(result);
Assert.assertEquals(3,result);
}
@Test
public void testSub(){
Calculator c = new Calculator();
System.out.println("testSub...");
int result = c.sub(1,2);
System.out.println(result);
Assert.assertEquals(-1,result);
}
}
3、模拟Post/Get请求
3.1 TestRestTemplate 方式:
package com.lydms.demojunit;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@SpringBootTest
class DemoJunitApplicationTests {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
void contextLoads() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("token", "3dvcmQ6c3dvcmRfc2VjcmV0");
JSONObject body = new JSONObject();
body.put("tim", "314");
body.put("tim", "314");
HttpEntity<String> formEntity = new HttpEntity<>(body.toString(), headers);
String postResult = testRestTemplate.postForObject("/test/url", formEntity, String.class);
String getResult = testRestTemplate.getForObject("/test/url", String.class);
}
}
3.2 WebApplicationContext 方式:
package com.lydms.demojunit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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;
@SpringBootTest
class DemoJunitApplicationTests {
@Autowired
private WebApplicationContext wac;
@Test
void contextLoads() throws Exception {
MockMvc build = MockMvcBuilders.webAppContextSetup(wac).build();
MockHttpServletRequestBuilder post = MockMvcRequestBuilders.post("/post");
post.content("fff".getBytes());
String contentAsString = build.perform(post).andReturn().getResponse().getContentAsString();
System.out.println(contentAsString);
}
}
4、Assert (结果判断)
assertTrue :assertEquals :assertArrayEquals :assertNotNull :
Map map = mapper.readValue(postResult, Map.class);
Assert.assertTrue(postResult != null);
Assert.assertEquals("100", "100");
int[] ints = new int[]{1, 2, 3, 4};
Assert.assertArrayEquals(ints, ints);
Assert.assertNotNull("100");
|