2017-07-05 42 views
1

我無法驗證jsonPath是否正在爲我的控制器執行任何操作。我對測試領域很陌生,並且已經接受了推薦網站,但是設計中有很多非常有特色的內容,所以很難找到答案。如果有人能幫助我,我將不勝感激。代碼:測試Junit和Mockito在分析日期時出現錯誤

package test 

public class FormControllerTest { 

    private MockMvc mockMvc; 

    @Mock 
    private FormServiceImpl formService; 

    @Mock 
    private UserServiceImpl userService; 


    @InjectMocks 
    private FormController formController; 

    @Before 
    public void init() { 
    MockitoAnnotations.initMocks(this); 

    mockMvc = MockMvcBuilders 
      .standaloneSetup(formController) 
      .build(); 
    } 

測試:

@Test 
public void test_get_Form_success() throws Exception { 

    SimpleDateFormat formato = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); 

    Date data = formato.parse("2017-01-03 02:00:02"); 
    Date data2 = formato.parse("2017-01-05 02:00:02"); 

    Form form = new Form().id(1).name("formTest") 
      .active(true).description("Formtesting") 
      .dateInit(data).dateEnd(data2); 

    //List<Form> forms = Arrays.asList(form.dateInit(data).dateEnd(data2)); 
    //System.out.println(form.getDateEnd()); 

    when(formService.findOneForm(form.getId())).thenReturn(form); 

    mockMvc.perform(get("/form/{id}",form.getId())) 
      .andExpect(status().isOk()) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) 
      .andExpect(jsonPath("$.id", is(1))) 
      .andExpect(jsonPath("$.name", is("formTest"))) 
      .andExpect(jsonPath("$.active", is(true))) 
      .andExpect(jsonPath("$.description", is("Formtesting"))) 
      .andExpect(jsonPath("$.dateInit", is(form.getDateInit()))) 
      .andExpect(jsonPath("$.dateEnd", is(form.getDateEnd()))); 

    verify(formService, times(2)).findOneForm(form.getId()); 
    verifyNoMoreInteractions(formService); 

} 

錯誤

java.lang.AssertionError: JSON path "$.dateInit" Expected: is Tue (Jan 03 02:00:02 BRST 2017) but: was "2017-01-03 04:00:02"

然後它增加了2小時的執行結束,如何解決這個問題呢?

回答