SpringBoot入门系列:第七篇 Spring Boot的测试

    Spring Boot的测试,和普通项目的测试类同,可以继续使用我们熟悉的测试工具。当然,这里的测试,主要还是后台代码的测试。

主要需要注意的地方仅有三点:

1、依赖包的引入:pom.xml中仅依赖spring-boot-starter-test,它把相关的依赖全部引入。

2、在测试类上的注解,常用的注解有三个

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration

这三个注解,只要注意第二个注解括号内的Application.class就行,把它替换为项目的启动类即可。

我们前面spring-boot-sample-mysql工程的启动类为SpringBootSampleMysqlApplication,那么测试类上的注解就是

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootSampleMysqlApplication.class)
@WebAppConfiguration

3、测试类的文件结构,保持src/test/java和src/main/java结构一直,即:包+文件夹。

     如:com.example包service中类的测试,那么在src/test/java也是建立com.example包,再在包下建立文件夹service.


注:由于我们测试的启动类用的是项目的启动类,所以Spring Boot项目的测试配置文件任然用src/main/resources的。

下面贴两个个测试类代码

1、服务类的

package com.example.service;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.example.Application;
import com.example.domain.TestPOJO;
import com.example.dto.HotelDto;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class TestServicesTest {

	@Autowired
	TestServices testServices;
	
	@Test
	public void testShow() {
		String expectedResult="hello world!";
		String result=testServices.show();
		Assert.assertTrue("数据一致", expectedResult.equals(result));
		Assert.assertFalse("数据不一致", !expectedResult.equals(result));
	}

	@Test
	public void testShowDao() {
		List testPOJOList=testServices.showDao(10);
		Assert.assertTrue("数据集不对", testPOJOList.size()==1);
		Assert.assertTrue("数据一致", testPOJOList.get(0).getName().equals("nice"));
	}

	@Test
	public void testFindByCountry() {
		List testPOJOList=testServices.findByCountry("US");
		Assert.assertTrue("数据集不对", testPOJOList.size()==1);
		Assert.assertTrue("数据一致", testPOJOList.get(0).getCityName().equals("San Francisco"));
	}

}
2、controller类的

package com.example.controller;

import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
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.Application;
import com.example.domain.TestPOJO;
import com.example.dto.HotelDto;
import com.example.service.TestServices;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class TestControllerTest {

	MockMvc mvc;

	@Autowired
	WebApplicationContext webApplicationConnect;

	@Autowired
	TestServices testServices;

	String expectedJson;

	@Before
	public void setUp() throws JsonProcessingException {
		mvc = MockMvcBuilders.webAppContextSetup(webApplicationConnect).build();

	}

	@Test
	public void testShow() throws Exception {
		String expectedResult = "hello world!";
		String uri = "/show";
		MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))
				.andReturn();
		int status = mvcResult.getResponse().getStatus();
		String content = mvcResult.getResponse().getContentAsString();

		Assert.assertTrue("错误,正确的返回值为200", status == 200);
		Assert.assertFalse("错误,正确的返回值为200", status != 200);
		Assert.assertTrue("数据一致", expectedResult.equals(content));
		Assert.assertFalse("数据不一致", !expectedResult.equals(content));
	}

	protected String Obj2Json(Object obj) throws JsonProcessingException {
		ObjectMapper mapper=new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	@Test
	public void testShowDaoInt() throws Exception {
		List testPOJOList = testServices.showDao(10);
		expectedJson = Obj2Json(testPOJOList);

		String uri="/showDao?age=10";
		MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();
		int status=mvcResult.getResponse().getStatus();
		String content=mvcResult.getResponse().getContentAsString();
		
		Assert.assertTrue("错误,正确的返回值为200", status == 200);
		Assert.assertFalse("错误,正确的返回值为200", status != 200);
		Assert.assertTrue("数据一致", expectedJson.equals(content));
		Assert.assertFalse("数据不一致", !expectedJson.equals(content));
	}

	@Test
	public void testShowDaoString() throws Exception {
		List hotelDtoList=testServices.findByCountry("US");
		expectedJson = Obj2Json(hotelDtoList);
		
		String uri="/country/US";
		MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();
		int status=mvcResult.getResponse().getStatus();
		String content=mvcResult.getResponse().getContentAsString();
		
		Assert.assertTrue("错误,正确的返回值为200", status == 200);
		Assert.assertFalse("错误,正确的返回值为200", status != 200);
		Assert.assertTrue("数据一致", expectedJson.equals(content));
		Assert.assertFalse("数据不一致", !expectedJson.equals(content));
	}

}
controller类的,为了MockMvc,注入了WebApplicationContext。

你可能感兴趣的:(springboot,springboot)