2016-12-23 72 views
-4

我從我的控制器創建測試,它必須將重定向url返回到「/ register/duplicate」。但是當我執行該操作時,我會遇到異常,例如DataIntegrityViolationException。控制器應該抓住這個例外,但不抓住Mockito和預計的例外

它是如何工作的?而我能做些什麼,到控制器的異常已經發生,並且在「捕獲」事業執行代碼,將被重定向到/註冊/複製

測試:

@Test 
public void testRegisterPageLogic_Duplicate() throws Exception { 
    expectedUser = new User("Jorg", "Brush"); 
    UserService mockService = mock(UserService.class); 

    userService.add(expectedUser); 

    when(mockService.add(expectedUser)).thenCallRealMethod(); 

    registerController = new RegisterController(mockService); 
    mockMvc = standaloneSetup(registerController).build(); 

    mockMvc.perform(post("/register") 
      .param("hide", "up") 
      .param("username", expectedUser.getUsername()) 
      .param("password", expectedUser.getPassword())) 
      .andExpect(redirectedUrl("/register/duplicate")); 

    Mockito.verify(mockService, atLeastOnce()).add(expectedUser); 

    userService.remove(userService.findUser("Jorg").getUserId()); 
} 

控制器的方法:

@RequestMapping(method = POST) 
public String register(@RequestParam("hide") String hide, 
         @RequestParam("username") String username, 
         @RequestParam("password") String password) { 

    if (hide.equals("up")) { 
     try { 
      userService.add(new User(username, password)); 
     } catch (DataIntegrityViolationException e) { 
      return "redirect:/register/duplicate"; 
     } 
    } 
    User user = userService.findUser(username); 
    if (user == null) { 
     return "redirect:/register/notExists"; 
    } 
    UserSession.setUser(user); 
    UserSession.setId(user.getUserId()); 
    return "redirect:/notes/" + UserSession.getUser().getUsername(); 
} 
+0

我已閱讀你的問題兩次,對不起,我不明白你想要什麼 –

+0

你問如何使用mockito使一些模擬拋出異常? –

+0

即使我不明白這個問題。 –

回答

0

讓我們儘量簡化,如果代碼是:

try { 
    userService.add(new User(username, password)); 
} catch (DataIntegrityViolationException e) { 
    return "redirect:/register/NOT-OK"; 
} 
return "redirect:/register/OK"; 

你想測試兩個分支,然後

@RunWith(MockitoJUnitRunner.class) // for @Mock 
class TheTest { 

    @Mock 
    UserService mockService; 

    MockMvc mockMvc;  

    @Before 
    public void setup() {   
     RegisterController registerController = new RegisterController(mockService); 
     mockMvc = standaloneSetup(registerController).build(); 
    } 

    @Test 
    public void testOK() throw Exception { 
     // mockService does nothing by default 

     mockMvc.perform(post("/register")) 
       .andExpect(view().name("/register/OK")); 

     Mockito.verify(mockService).add(Matchers.any(User.class)); 
    } 

    @Test 
    public void testNotOk() { 
     Mockito.when(mockService.add(Matchers.any(User.class)) 
       .thenThrow(new DataIntegrityViolationException()); // adjust DataIntegrityViolationException constructor args if required. 

     // now mockService throw a DataIntegrityViolationException when mockService#add(User) is called 

     mockMvc.perform(post("/register")) 
       .andExpect(view().name("/register/NOT-OK")); 

     Mockito.verify(mockService).add(Matchers.any(User.class)); 
    } 
} 
+0

因爲'new User()'每次給你一個全新的用戶 –

0

要在Mockito中引發異常,您不需要調用真正的方法。您可以使用此代碼:

when(mockService.add(expectedUser)).thenThrow(DataIntegrityViolationException.class) 
+0

我試過了,但不同的是,新的DataIntegrityViolationException 「」)。但是,在控制器中執行此方法'mockService.add(expectedUser)'時,此try {catch}會被忽略並繼續運行。然後,我在測試中發現一個錯誤:預期:/ register/duplicate Actual:/ register/notExists – MrChebik