1. 程式人生 > >使用spring-boot-test做測試

使用spring-boot-test做測試

一,控制層測試

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();
		}
	}
}

三,資料參考