如何为该控制器编写Junit测试用例?
@PostMapping(path = "/appformsubmission")
public AppFormChannelResponseObject saveAppForm(
@RequestBody AppFormChannelRequestObject<AppFormDetails> requestObject) throws JsonProcessingException {
logger.info("MwController -saveAppForm ");
if (logger.isDebugEnabled()) {
logger.debug("Entering MwController() method");
logger.debug("requestObject : {}", Utility.toJsonString(requestObject));
}
return appFormService.submitApplicationForm(requestObject);
}
如果我是Junit的新手,如果我得到一个示例测试用例,那将很棒。预先感谢。
答案 0 :(得分:1)
在Spring Boot文档中,通过使用MockMvc进行MVC层测试
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class AppFormChannelTest{
@Autowired
private MockMvc mvc;
@Test
public void saveAppFormTest() throws Exception {
AppFormChannelRequestObject body=new AppFormChannelRequestObject();
Gson gson = new Gson();
String json = gson.toJson(body);
this.mockmvc.perform(post("/appformsubmission/")
.contentType(MediaType.APPLICATION_JSON).content(json))
.andExpect(status().isOk());
}
}
答案 1 :(得分:0)