最終更新: chipstar_light 2010年10月20日(水) 22:22:20履歴
戻り値を操作する
when(request.getSession()).thenReturn(new HttpSession()); when(request.getAttribuete("キー").thenThrow(new RuntimeException());voidメソッドが投げる例外を操作する
doThrow(new RuntimeException()).when(request).getAttribuete("キー");任意のメソッドを実行した結果を戻り値として返す
// ※anyString()は任意の文字列という意味 when(request.getParameter(anyString())).thenAnswer(new Answer<String>() { public String answer(InvocationOnMock invocation) throws Throwable { String key = (String) invocation.getArguments()[0]; return key + "の値"; } });
verify(request).getAttribute("キー");複数回呼ばれる可能性がある場合に、最低1回は呼ばれる事を確認する
verify(request, Mockito.atLeastOnce()).getAttribute("キー");呼ばれた順番を確認する
InOrder order = inOrder(request, オブジェクト2, オブジェクト3,...); order.verify(request).getAttribute("key1"); order.verify(request).getAttribute("key2"); order.verify(オブジェクト2).someMethod();不特定な引数で呼び出された事を確認する
※他にも、any()<-オブジェクト、any(Class<T>)<-任意の型のインスタンス、anyInt()<-任意の数値などがある
verify(request).getAttribute(anyString());呼び出されたメソッドに渡された引数の状態を確認する
ArgumentCaptor<Hoge> argument = new ArgumentCaptor<Hoge>(); verify(foo).someMethod(argument.capture()); Hoge hoge = argument.getValue(); assertEquals("XXX", hoge.toString());呼び出されていない事を確認する
verify(request, never()).getAttribute(any(String.class));
コメントをかく