SpringBoot 单元测试
单元测试(模块测试)是开发者编写的一小段代码,用于检验被测代码的一个很小的、很明确的功能是否正确。
单元测试是由程序员自己来完成,最终受益的也是程序员自己。可以这么说,程序员有责任编写功能代码,同时也就有责任为自己的代码编写单元测试。
执行单元测试,就是为了证明这段代码的行为和我们期望的一致。
对于程序员来说,如果养成了对自己写的代码进行单元测试的习惯,不但可以写出高质量的代码,而且还能提高编程水平。
对Controller层进行单元测试
1 @RunWith(SpringRunner.class) 2 @WebAppConfiguration 3 @SpringBootTest(classes = AuthServerApplication.class) 4 public class BaseControllerTest { 5 protected Logger logger = LoggerFactory.getLogger(BaseControllerTest.class); 6 7 @Before 8 public void init() { 9 logger.info("开始测试..."); 10 } 11 12 @After 13 public void after() { 14 logger.info("测试结束..."); 15 } 16 }
1 public class LoginControllerTest extends BaseControllerTest { 2 private MockMvc mockMvc; 3 @Autowired 4 private WebApplicationContext wac; 5 @Before 6 public void setup(){ 7 mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); 8 } 9 10 11 @Test 12 public void login() throws Exception { 13 MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/login/getTemporaryToken") 14 .accept(MediaType.APPLICATION_JSON) 15 .contentType(MediaType.APPLICATION_JSON)) 16 .andDo(MockMvcResultHandlers.print()) 17 .andReturn(); 18 int status=mvcResult.getResponse().getStatus(); 19 Assert.assertEquals(ResultCode.OK, status); 20 if(ResultCode.OK != status){ 21 logger.error("/login/getTemporaryToken:" + mvcResult.getResponse().getContentAsString()); 22 return; 23 } 24 ResultData rd = JSON.parseObject(mvcResult.getResponse().getContentAsString(),ResultData.class); 25 Assert.assertEquals(ResultCode.OK, (int)rd.getCode()); 26 TemporaryTokenDTO tokenDTO = JSON.parseObject(rd.getData().toString(),TemporaryTokenDTO.class); 27 LoginDTO loginDTO = new LoginDTO(); 28 loginDTO.setAccount("hujinshui"); 29 loginDTO.setPassword("123456"); 30 MvcResult loginResult = mockMvc.perform(MockMvcRequestBuilders.post("/login/in") 31 .accept(MediaType.APPLICATION_JSON) 32 .contentType(MediaType.APPLICATION_JSON) 33 .content(JSONObject.toJSONString(loginDTO)) 34 .header("temporaryToken",tokenDTO.getTemporaryToken())) 35 .andDo(MockMvcResultHandlers.print()) 36 .andReturn(); 37 int loginStatus=loginResult.getResponse().getStatus(); 38 if(ResultCode.OK != loginStatus){ 39 logger.error("/login/in:" + loginResult.getResponse().getContentAsString()); 40 } 41 Assert.assertEquals(ResultCode.OK, loginStatus); 42 } 43 }
对service层进行测试
1 @RunWith(SpringRunner.class) 2 @SpringBootTest(classes = AuthServerApplication.class) 3 public class BaseServiceTest { 4 protected Logger logger = LoggerFactory.getLogger(BaseServiceTest.class); 5 6 @Before 7 public void init() { 8 logger.info("service测试开始..."); 9 } 10 11 @After 12 public void after() { 13 logger.info("service测试结束..."); 14 } 15 }
1 public class LoginServiceTest extends BaseServiceTest { 2 @Autowired 3 private LoginService loginService; 4 private MockHttpServletRequest request; 5 private MockHttpServletResponse response; 6 7 @Before 8 public void setUp(){ 9 request = new MockHttpServletRequest(); 10 request.setCharacterEncoding("UTF-8"); 11 response = new MockHttpServletResponse(); 12 } 13 14 @Test 15 public void getTemporaryToken(){ 16 ResultData rd = loginService.getTemporaryToken(); 17 Assert.assertEquals(ResultCode.OK, (int)rd.getCode()); 18 logger.info("LoginService getTemporaryToken:"+JSON.toJSONString(rd)); 19 } 20 21 @Test 22 public void login(){ 23 ResultData rd = loginService.login("hujinshui","123456", request); 24 Assert.assertEquals(ResultCode.OK, (int)rd.getCode()); 25 logger.info("LoginService login:"+JSON.toJSONString(rd)); 26 } 27 28 @Test 29 public void test(){ 30 getTemporaryToken(); 31 login(); 32 } 33 34 }
对dao层进行测试
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 public class BaseDaoTest { 4 5 protected Logger logger = LoggerFactory.getLogger(BaseDaoTest.class); 6 7 @Before 8 public void init() { 9 logger.info("dao测试开始..."); 10 } 11 12 @After 13 public void after() { 14 logger.info("dao测试结束..."); 15 } 16 }
1 public class RoleDaoTest extends BaseDaoTest{ 2 @Autowired 3 private RoleDao roleDao; 4 5 @Test 6 public void test() { 7 Role role = roleDao.selectByIfDefault(1); 8 Assert.assertEquals((int)role.getIfDefault(),1); 9 logger.info("selectByIfDefault :" + JSON.toJSONString(role)); 10 11 Role role2 = roleDao.selectByPrimaryKey(role.getId()); 12 Assert.assertEquals(role.getId(),role2.getId()); 13 logger.info("selectByPrimaryKey :" + JSON.toJSONString(role2)); 14 } 15 }