2016-12-23 94 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