Junit测试类使用

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提供的测试引擎都可以。指定运⾏测

试的引擎

  • @SpringBootTest的属性值指的是引导类的字节码对象

  • 注意:最新版的2.2.0.RELEASE中,springboot测试类不再需要**@Runwith**的注解

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....");
    }

    /**
     * 测试testAdd()方法
     */
    @Test
    public void testAdd(){
        //System.out.println("我被执行了");
        //1,创建计算器对象
        System.out.println("testAdd...");
        Calculator c = new Calculator();
        //2,调用add方法
        int result = c.add(1,2);
        System.out.println(result);

        //3,断言,我断言这个结果的3
        Assert.assertEquals(3,result);//期望值是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");

//        请求Body体
        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);
//  结果是否为true
Assert.assertTrue(postResult != null);
//  是否相等
Assert.assertEquals("100", "100");
int[] ints = new int[]{1, 2, 3, 4};
Assert.assertArrayEquals(ints, ints);
//  不为空
Assert.assertNotNull("100");
posted @ 2022-08-03 23:53  ah_lydms  阅读(74)  评论(0编辑  收藏  举报