springboot之单元测试
springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例
工程层次结构如图
代码如下:
controller:
package com.rookie.bigdata.controller; import com.rookie.bigdata.domain.User; import com.rookie.bigdata.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * controller层 * Created by on 2018/9/28. */ @RestController public class UserController { @Autowired private UserService userService; /** * 查询用户 * * @return */ @GetMapping(value = "/user") public User findUser() { return userService.findOne(10); } }
User:
package com.rookie.bigdata.domain; /** * domain实体对象 * Created by on 2018/9/28. */ public class User { private int id; private String name; private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
service:
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.User; import org.springframework.stereotype.Service; /** * service层 * Created by on 2018/9/28. */ @Service public class UserService { public User findOne(Integer id) { User user = new User(); user.setId(id); user.setName("张三"); user.setAge(23); return user; } }
启动程序:
package com.rookie.bigdata; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; /** * 应用程序启动类 * Created by on 2018/8/2. */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
测试类:
package com.rookie.bigdata.controller; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; 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.result.MockMvcResultMatchers; import static org.junit.Assert.*; /** * 测试contoller层 * Created by on 2018/9/28. */ @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class UserControllerTest { @Autowired private MockMvc mvc; @Test public void findUser() throws Exception { // mvc.perform(MockMvcRequestBuilders.get("/user")) // .andExpect(MockMvcResultMatchers.status().isOk()); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/user")) .andExpect(MockMvcResultMatchers.status().isOk())//模拟发送get请求 .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))//预期返回值的媒体类型 application/json;charset=UTF-8 .andReturn();//返回执行的请求结果 System.out.println(mvcResult.getResponse().getContentAsString()); } }
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.User; 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.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * 测试service层 * Created by on 2018/9/28. */ @RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTest { @Autowired private UserService userService; @Test public void findOne() throws Exception { User user = userService.findOne(1); Assert.assertEquals(new Integer(23),user.getAge()); } }