springboot测试类

mac2022-06-30  26

Controller测试类

/** * Created by zhiqi.shao on 2017/5/12. */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes =MelonApplication.class) @WebAppConfiguration //启动一个真实web服务,然后调用Controller的Rest API,待单元测试完成之后再将web服务停掉 public class TestUserController { @Autowired protected WebApplicationContext wac; protected MockMvc mockMvc; //private TestRestTemplate restTemplate = new TestRestTemplate(); @Before public void setup() throws IOException { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void testf() throws Exception{ String updateResult = mockMvc.perform(MockMvcRequestBuilders.post("/admin/test").param("id", "4")) .andReturn() .getResponse() .getContentAsString(); System.out.println("----------查询----------" + updateResult); HttpServletResponse response= mockMvc.perform(MockMvcRequestBuilders.post("/admin/test").param("id", "4")) .andReturn() .getResponse(); System.out.println("***************************************************"+response); } }

Service测试类

/** * Created by zhiqi.shao on 2017/5/12. */ @Slf4j @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) public class TestUserService { @Autowired private UserService userService; private Long id; @Before public void bf(){ log.info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
转载请注明原文地址: https://mac.8miu.com/read-73892.html
最新回复(0)