版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36361038/article/details/80725132
1、对service的测试
package com.xb; import com.xb.entity.Girl; import com.xb.service.GirlService; import org.junit.*; 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; /** * Created by XueBiao on 2018/6/18. */ @RunWith(SpringRunner.class) //将启动整个springboot工程 @SpringBootTest public class GirlServiceTest { @Autowired private GirlService girlService; @Test public void finfOneTest(){ Girl girl=girlService.finfOne(1); Assert.assertEquals(new Integer(23),girl.getAge()); } }
2、对controller的测试
package com.xb.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.autoconfigure.web.servlet.AutoConfigureWebMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.junit.Assert.*; /** * Created by XueBiao on 2018/6/18. */ @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class GirlControllerTest { @Autowired private MockMvc mvc; @Test public void girlList() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/girlList")) .andExpect(MockMvcResultMatchers.status().isOk());//对返回的转态码进行判断 } }