我使用以下junit测试来测试save方法,它运行良好并在测试http状态时获得200 ok,我测试实体是否已创建,现在我需要测试创建的实体,测试它的外观。有人可以帮帮我吗?
@Test
public void testContact() throws Exception {
long before = contactRepository.count();
ContactResource r = new ContactResource();
r.name = "name";
r.email = "some-one@xxx.com";
ResultActions perform = mockMvc.perform(
post(ContactWebservice.URL)
.content(r)
.contentType(MediaType.APPLICATION_JSON)
).andDo(print());
long after = contactRepository.count();
assertEquals("contact should be saved", before+1, after);
perform.andExpect(status().isOk());
}
方法实现
@Override
public void post(@RequestBody @Valid ContactResource resource){
Contact registration = assembler.toEntity(resource);
contactRepo.save(registration);
Resource bodyR = loader.getResource(
EuromakBackendConfig.EMAIL_TEMPLATE_CONTACT);
String subject = "Thanks for registering";
String html = support.toString(bodyR);
service.sendHtmlMail(from, registration.email, subject, html);
}
接口
@RequestMapping(method = RequestMethod.POST)
public void post(@RequestBody ContactResource resource);