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

その後、両方枝を、テストしたいですあなたの質問を2回、そして申し訳ありませんが、私はあなたが欲しいものを理解していません –

+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

とテストでエラーが発生します。 –

0

Mockitoで例外を発生させるには、実際のメソッドを呼び出す必要はありません。あなたはこのコードを使用することができます:

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

私はそれを試みましたが、新しいDataIntegrityViolationException "")。しかし、このメソッド 'mockService.add(expectedUser)'を実行すると、コントローラ内でこのtry {} catchは無視され、実行を続けます。 'new User()'は毎回まったく新しいユーザーを与えるため、期待通りに/ register/duplicate Actual:/ register/notExists – MrChebik

関連する問題