junit测试以测试保存方法

时间:2015-06-06 13:11:36

标签: java spring junit

我使用以下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);

0 个答案:

没有答案
相关问题