欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用spring-boot-test做测试

程序员文章站 2022-03-07 22:37:55
...

一,控制层测试

package demo.springdatajap.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@RequestMapping(name="/",method=RequestMethod.GET)
	public String get(String name) {
		return "hello,"+name;
	}
	
	@RequestMapping(name="/",method=RequestMethod.POST)
	public String post(String name) {
		return "hello,"+name;
	}
}
package demo.springdatajap.test;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import demo.springdatajap.SpringbootApplication;

@RunWith(SpringRunner.class)
//classes = SpringbootApplication.class SpringbootApplication为启动类
@SpringBootTest(classes = SpringbootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest {

    /**
     * 端口号
     */
    @LocalServerPort
    private int port;
    /**
     * 基础路径
     */
    private URL base;

    @Autowired
    private TestRestTemplate restTemplate;

    @Before
    public void init() throws Exception {
        String url = String.format("http://localhost:%d/", port);
        this.base = new URL(url);
    }

    /**
     * get请求测试
     * @throws Exception
     */
    @Test
    public void get() throws Exception {
    	//请求路径
    	String url = this.base.toString()+"user?name={name}";
    	//请求参数
    	Map<String, String> urlVariables = new HashMap<>();
    	urlVariables.put("name", "get");
    	
        ResponseEntity<String> response = this.restTemplate.getForEntity(url, String.class,urlVariables);
        System.out.println("测试结果为:" + response.getBody());
    }
    
    /**
     * post请求测试
     * @throws Exception
     */
    @Test
    public void post() throws Exception {
    	//请求路径
    	String url = this.base.toString()+"user";
    	//请求参数
    	MultiValueMap<String, String> urlVariables = new LinkedMultiValueMap<String, String>();
    	urlVariables.add("name", "post");
    	
        ResponseEntity<String> response = this.restTemplate.postForEntity(url, urlVariables, String.class);
        System.out.println("测试结果为:" + response.getBody());
    }
    
}

二,业务层测试

package demo.springdatajap.test;

import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import demo.springdatajap.SpringbootApplication;
import demo.springdatajap.entity.User;
import demo.springdatajap.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
//classes = SpringbootApplication.class SpringbootApplication为启动类
@SpringBootTest(classes = SpringbootApplication.class)
public class UserServiceTest {
	
	@Autowired
	private UserService userService;
	
	@Test
	public void test() {
		try {
			List<User> list = userService.findUserList();
			for (User user : list) {
				System.out.println(user.getName());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

三,资料参考

使用@SpringBootTest注解进行单元测试:https://blog.csdn.net/limenghua9112/article/details/79694849

Spring Boot 单元测试详解+实战教程:https://www.cnblogs.com/javastack/p/9150408.html

springboot测试service层的单元测试:https://www.cnblogs.com/xubiao/p/7568257.html

相关标签: 测试

上一篇: dokcer入门

下一篇: springboot test