Mockitoの浅談
11334 ワード
Mockitoの浅談
[TOC]
Mockitoの概要
Mockのメリットは何ですか?
これは上の点に似ています.存在しないコードのテストを作成します.しかし、開発者がテストプログラムを作成することについては、テストチームが作成することについて説明しています.まだ測定するものがない場合、テストチームはどのようにテストを作成しますか?シミュレーションとシミュレーションテスト!これは、サービスがテストを必要とすると言い訳した場合、実際にQAチームには完全なテストコンポーネントがあります.1つのチームが別のチームの完了を待つことはありません.これにより,シミュレーションの効率型が特に際立った.
Mocksは非常に効率的であるため、概念証明書を作成したり、概略図として使用したり、プロジェクトの構築を検討しているデモプログラムとして使用したりすることができます.これは、プロジェクトが次に行うかどうかを決定するために強力な基礎を提供していますが、最も重要なのは実際の設計決定を提供しています.
場合によっては、パートナーやお客様などの外部ソースからテストシステムにアクセスできるようにする必要があります.これらの理由により、他の人も機密情報にアクセスできますが、一部のテスト環境にアクセスできるようにしたいだけかもしれません.この場合、パートナーまたはお客様にテストシステムを提供して、開発またはテストを行うにはどうすればいいですか?最も簡単なのは、あなたのネットワークやお客様からのネットワークにかかわらず、mockを提供することです.soapUI mockは構成が簡単で、soapUIを実行したり、warパッケージとしてjavaサーバに公開したりすることができます.
Mockitoの使用例
シミュレーションオブジェクト
// LinkedList
LinkedList mockedList = mock(LinkedList.class);
// get , null,
System.out.println(mockedList.get(0));
シミュレーションメソッド呼び出しの戻り値
// , first。 stub。
when(mockedList.get(0)).thenReturn("first");
// first
System.out.println(mockedList.get(0));
シミュレーションメソッド呼び出し放出例外
// , RuntimeException
when(mockedList.get(1)).thenThrow(new RuntimeException());
// RuntimeException
System.out.println(mockedList.get(1));
関数に値タイプが返されていない場合は、このメソッドを使用して例外放出をシミュレートできます.doThrow(new RuntimeException("clear exception")).when(mockedList).clear();
mockedList.clear();
シミュレーション呼び出しメソッド時のパラメータマッチング
// anyInt() int , , element
when(mockedList.get(anyInt())).thenReturn("element");
// element
System.out.println(mockedList.get(999));
シミュレーションメソッド呼び出し回数
// add
mockedList.add("once");
// , add
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
けんさどうさ
// mock creation
List mockedList = mock(List.class);
// using mock object
mockedList.add("one");
mockedList.clear();
//verification
verify(mockedList).add("one");
verify(mockedList).clear();
シミュレーションメソッド呼び出し(Stubbing)
//You can mock concrete classes, not just interfaces
LinkedList mockedList = mock(LinkedList.class);
//stubbing
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints "first"
System.out.println(mockedList.get(0));
//following throws runtime exception
System.out.println(mockedList.get(1));
//following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));
verify(mockedList).get(0);
パラメータマッチング
//stubbing using built-in anyInt() argument matcher
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
when(mockedList.contains(argThat(isValid()))).thenReturn("element");
//following prints "element"
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());
//argument matchers can also be written as Java 8 Lambdas
verify(mockedList).add(someString -> someString.length() > 5);
チェックメソッド呼び出し回数
//using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//following two verifications work exactly the same - times(1) is used by default
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//exact number of invocations verification
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost()
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("five times");
verify(mockedList, atMost(5)).add("three times");
シミュレーション戻りメソッドなしで例外を放出
doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();
メソッド呼び出し順序の検証
// A. Single mock whose methods must be invoked in a particular order
List singleMock = mock(List.class);
//using a single mock
singleMock.add("was added first");
singleMock.add("was added second");
//create an inOrder verifier for a single mock
InOrder inOrder = inOrder(singleMock);
//following will make sure that add is first called with "was added first, then with "was added second"
inOrder.verify(singleMock).add("was added first");
inOrder.verify(singleMock).add("was added second");
// B. Multiple mocks that must be used in a particular order
List firstMock = mock(List.class);
List secondMock = mock(List.class);
//using mocks
firstMock.add("was called first");
secondMock.add("was called second");
//create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = inOrder(firstMock, secondMock);
//following will make sure that firstMock was called before secondMock
inOrder.verify(firstMock).add("was called first");
inOrder.verify(secondMock).add("was called second");
// Oh, and A + B can be mixed together at will
メソッドが呼び出されていないかどうかを確認
//using mocks - only mockOne is interacted
mockOne.add("one");
//ordinary verification
verify(mockOne).add("one");
//verify that method was never called on a mock
verify(mockOne, never()).add("two");
//verify that other mocks were not interacted
verifyZeroInteractions(mockTwo, mockThree);
Mockオブジェクトの迅速な作成
public class ArticleManagerTest {
@Mock private ArticleCalculator calculator;
@Mock private ArticleDatabase database;
@Mock private UserProvider userProvider;
@Before
public void before(){
MockitoAnnotations.initMocks(this);
}
}
カスタムは異なる結果を返します
when(mock.someMethod("some arg"))
.thenThrow(new RuntimeException()) //
.thenReturn("foo"); //
//First call: throws runtime exception:
mock.someMethod("some arg"); //
//Second call: prints "foo"
System.out.println(mock.someMethod("some arg")); //
//Any consecutive call: prints "foo" as well (last stubbing wins).
System.out.println(mock.someMethod("some arg")); // n (n> 2),
戻り結果をブロック
when(mock.someMethod(anyString())).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Object mock = invocation.getMock();
return "called with arguments: " + args;
}
});
//the following prints "called with arguments: foo"
System.out.println(mock.someMethod("foo"));
Mock関数操作
定義関数操作は、doThrow()、doAnswer()、doNothing()、doReturn()and doCallRealMethod()を使用して実行できます.
実際のオブジェクトをひそかに呼び出す
List list = new LinkedList();
List spy = spy(list);
//optionally, you can stub out some methods:
when(spy.size()).thenReturn(100);
//using the spy calls *real* methods
spy.add("one");
spy.add("two");
//prints "one" - the first element of a list
System.out.println(spy.get(0));
//size() method was stubbed - 100 is printed
System.out.println(spy.size());
//optionally, you can verify
verify(spy).add("one");
verify(spy).add("two");
デフォルトの戻り値を変更
Foo mock = mock(Foo.class, Mockito.RETURNS_SMART_NULLS);
Foo mockTwo = mock(Foo.class, new YourOwnAnswer());
関数のパラメータ値の取得
ArgumentCaptor argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());
部分ロック
//you can create partial mock with spy() method:
List list = spy(new LinkedList());
//you can enable partial mock capabilities selectively on mocks:
Foo mock = mock(Foo.class);
//Be sure the real implementation is 'safe'.
//If real implementation throws exceptions or depends on specific state of the object then you're in trouble.
when(mock.someMethod()).thenCallRealMethod();
ロックをリセット
List mock = mock(List.class);
when(mock.size()).thenReturn(10);
mock.add(1);
reset(mock);
//at this point the mock forgot any interactions & stubbing
シーケンス化
List
チェックタイムアウト
//passes when someMethod() is called within given time span
verify(mock, timeout(100)).someMethod();
//above is an alias to:
verify(mock, timeout(100).times(1)).someMethod();
//passes when som`eMethod() is called *exactly* 2 times within given time span
verify(mock, timeout(100).times(2)).someMethod();
//passes when someMethod() is called *at least* 2 times within given time span
verify(mock, timeout(100).atLeast(2)).someMethod();
//verifies someMethod() within given time span using given verification mode
//useful only if you have your own custom verification modes.
verify(mock, new Timeout(100, yourOwnVerificationMode)).someMethod();
Mock詳細
Mockito.mockingDetails(someObject).isMock();
Mockito.mockingDetails(someObject).isSpy();
// LinkedList
LinkedList mockedList = mock(LinkedList.class);
// get , null,
System.out.println(mockedList.get(0));
// , first。 stub。
when(mockedList.get(0)).thenReturn("first");
// first
System.out.println(mockedList.get(0));
// , RuntimeException
when(mockedList.get(1)).thenThrow(new RuntimeException());
// RuntimeException
System.out.println(mockedList.get(1));
doThrow(new RuntimeException("clear exception")).when(mockedList).clear();
mockedList.clear();
// anyInt() int , , element
when(mockedList.get(anyInt())).thenReturn("element");
// element
System.out.println(mockedList.get(999));
// add
mockedList.add("once");
// , add
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
// mock creation
List mockedList = mock(List.class);
// using mock object
mockedList.add("one");
mockedList.clear();
//verification
verify(mockedList).add("one");
verify(mockedList).clear();
//You can mock concrete classes, not just interfaces
LinkedList mockedList = mock(LinkedList.class);
//stubbing
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints "first"
System.out.println(mockedList.get(0));
//following throws runtime exception
System.out.println(mockedList.get(1));
//following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));
verify(mockedList).get(0);
//stubbing using built-in anyInt() argument matcher
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
when(mockedList.contains(argThat(isValid()))).thenReturn("element");
//following prints "element"
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());
//argument matchers can also be written as Java 8 Lambdas
verify(mockedList).add(someString -> someString.length() > 5);
//using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//following two verifications work exactly the same - times(1) is used by default
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//exact number of invocations verification
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost()
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("five times");
verify(mockedList, atMost(5)).add("three times");
doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();
// A. Single mock whose methods must be invoked in a particular order
List singleMock = mock(List.class);
//using a single mock
singleMock.add("was added first");
singleMock.add("was added second");
//create an inOrder verifier for a single mock
InOrder inOrder = inOrder(singleMock);
//following will make sure that add is first called with "was added first, then with "was added second"
inOrder.verify(singleMock).add("was added first");
inOrder.verify(singleMock).add("was added second");
// B. Multiple mocks that must be used in a particular order
List firstMock = mock(List.class);
List secondMock = mock(List.class);
//using mocks
firstMock.add("was called first");
secondMock.add("was called second");
//create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = inOrder(firstMock, secondMock);
//following will make sure that firstMock was called before secondMock
inOrder.verify(firstMock).add("was called first");
inOrder.verify(secondMock).add("was called second");
// Oh, and A + B can be mixed together at will
//using mocks - only mockOne is interacted
mockOne.add("one");
//ordinary verification
verify(mockOne).add("one");
//verify that method was never called on a mock
verify(mockOne, never()).add("two");
//verify that other mocks were not interacted
verifyZeroInteractions(mockTwo, mockThree);
public class ArticleManagerTest {
@Mock private ArticleCalculator calculator;
@Mock private ArticleDatabase database;
@Mock private UserProvider userProvider;
@Before
public void before(){
MockitoAnnotations.initMocks(this);
}
}
when(mock.someMethod("some arg"))
.thenThrow(new RuntimeException()) //
.thenReturn("foo"); //
//First call: throws runtime exception:
mock.someMethod("some arg"); //
//Second call: prints "foo"
System.out.println(mock.someMethod("some arg")); //
//Any consecutive call: prints "foo" as well (last stubbing wins).
System.out.println(mock.someMethod("some arg")); // n (n> 2),
when(mock.someMethod(anyString())).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Object mock = invocation.getMock();
return "called with arguments: " + args;
}
});
//the following prints "called with arguments: foo"
System.out.println(mock.someMethod("foo"));
List list = new LinkedList();
List spy = spy(list);
//optionally, you can stub out some methods:
when(spy.size()).thenReturn(100);
//using the spy calls *real* methods
spy.add("one");
spy.add("two");
//prints "one" - the first element of a list
System.out.println(spy.get(0));
//size() method was stubbed - 100 is printed
System.out.println(spy.size());
//optionally, you can verify
verify(spy).add("one");
verify(spy).add("two");
Foo mock = mock(Foo.class, Mockito.RETURNS_SMART_NULLS);
Foo mockTwo = mock(Foo.class, new YourOwnAnswer());
ArgumentCaptor argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());
//you can create partial mock with spy() method:
List list = spy(new LinkedList());
//you can enable partial mock capabilities selectively on mocks:
Foo mock = mock(Foo.class);
//Be sure the real implementation is 'safe'.
//If real implementation throws exceptions or depends on specific state of the object then you're in trouble.
when(mock.someMethod()).thenCallRealMethod();
List mock = mock(List.class);
when(mock.size()).thenReturn(10);
mock.add(1);
reset(mock);
//at this point the mock forgot any interactions & stubbing
List
//passes when someMethod() is called within given time span
verify(mock, timeout(100)).someMethod();
//above is an alias to:
verify(mock, timeout(100).times(1)).someMethod();
//passes when som`eMethod() is called *exactly* 2 times within given time span
verify(mock, timeout(100).times(2)).someMethod();
//passes when someMethod() is called *at least* 2 times within given time span
verify(mock, timeout(100).atLeast(2)).someMethod();
//verifies someMethod() within given time span using given verification mode
//useful only if you have your own custom verification modes.
verify(mock, new Timeout(100, yourOwnVerificationMode)).someMethod();
Mockito.mockingDetails(someObject).isMock();
Mockito.mockingDetails(someObject).isSpy();