官术网_书友最值得收藏!

Mockito

Mockito is a Java framework that allows easy creation of the test double.

The Gradle dependency is the following:

dependencies { 
  testCompile group: 'org.mockito', name: 'mockito-all', version: '1.+' 
} 

Mockito runs through the JUnit runner. It creates all the required mocks for us and injects them into the class with tests. There are two basic approaches; instantiating mocks by ourselves and injecting them as class dependencies via a class constructor or using a set of annotations. In the next example, we are going to see how it is done using annotations.

In order for a class to use Mockito annotations, it needs to be run with MockitoJUnitRunner. Using the runner simplifies the process because you just simply add annotations to objects to be created:

@RunWith(MockitoJUnitRunner.class) 
public class FriendshipsTest { 
... 
} 

In your test class, the tested class should be annotated with @InjectMocks. This tells Mockito which class to inject mocks into:

@InjectMocks 
FriendshipsMongo friendships; 

From then on, we can specify which specific methods or objects inside the class, in this case FriendshipsMongo, will be substituted with mocks:

@Mock 
FriendsCollection friends; 

In this example, FriendsCollection inside the FriendshipsMongo class will be mocked.

Now, we can specify what should be returned when friends is invoked:

Person joe = new Person("Joe"); 
doReturn(joe).when(friends).findByName("Joe"); 
assertThat(friends.findByName("Joe")).isEqualTo(joe); 

In this example, we're telling Mockito to return the joe object whenever friends.findByName("Joe") is invoked. Later on, we're verifying with assertThat that this assumption is correct.

Let's try to do the same test as we did previously in the class that was without MongoDB:

@Test 
public void joeHas5Friends() { 
  List<String> expected = 
Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul"); Person joe = spy(new Person("Joe")); doReturn(joe).when(friends).findByName("Joe"); doReturn(expected).when(joe).getFriends(); assertThat(friendships.getFriendsList("Joe")) .hasSize(5) .containsOnly("Audrey", "Peter", "Michael", "Britney", "Paul"); }

A lot of things happened in this small test. First, we're specifying that joe is a spy. In Mockito, spies are real objects that use real methods unless specified otherwise. Then, we're telling Mockito to return joe when the friends method calls getFriends. This combination allows us to return the expected list when the getFriends method is invoked. Finally, we're asserting that the getFriendsList returns the expected list of names.

The complete source code can be found in the FriendshipsMongoAssertJTest class in the https://bitbucket.org/vfarcic/tdd-java-ch02-example-junit repository.

We'll use Mockito later on; throughout this book, you'll get your chance to become more familiar with it and with mocking in general. More information about Mockito can be found at http://mockito.org/.

主站蜘蛛池模板: 闽侯县| 阿克苏市| 沾化县| 弋阳县| 汕头市| 阿坝县| 商水县| 光山县| 东乌珠穆沁旗| 龙口市| 北票市| 奇台县| 辉县市| 贵德县| 大连市| 康保县| 德庆县| 磐石市| 五家渠市| 辽阳县| 福州市| 武陟县| 襄城县| 芦溪县| 彭山县| 新和县| 扎鲁特旗| 龙口市| 婺源县| 浙江省| 蒙自县| 遵义市| 南华县| 射洪县| 塘沽区| 察隅县| 沁阳市| 桃园县| 姚安县| 尉氏县| 台南县|