- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 335字
- 2021-06-24 18:31:48
EasyMock
EasyMock is an alternative mocking framework. It is very similar to Mockito. However, the main difference is that EasyMock does not create spy objects but mocks. Other differences are syntactical.
Let's see an example of EasyMock. We'll use the same set of test cases as those that were used for the Mockito examples:
@RunWith(EasyMockRunner.class) public class FriendshipsTest { @TestSubject FriendshipsMongo friendships = new FriendshipsMongo(); @Mock(type = MockType.NICE) FriendsCollection friends;
}
Essentially, the runner does the same as the Mockito runner:
@TestSubject FriendshipsMongo friendships = new FriendshipsMongo(); @Mock(type = MockType.NICE) FriendsCollection friends;
The @TestSubject annotation is similar to Mockito's @InjectMocks, while the @Mock annotation denotes an object to be mocked in a similar fashion to Mockito's @Mock. Furthermore, the type NICE tells the mock to return empty.
Let's compare one of the asserts we did with Mockito:
@Test public void mockingWorksAsExpected() { Person joe = new Person("Joe"); expect(friends.findByName("Joe")).andReturn(joe); replay(friends); assertThat(friends.findByName("Joe")).isEqualTo(joe); }
Besides small differences in syntax, the only disadvantage of EasyMock is that the additional instruction replay was needed. It tells the framework that the previously specified expectation should be applied. The rest is almost the same. We're specifying that friends.findByName should return the joe object, applying that expectation and, finally, asserting whether the actual result is as expected.
In the EasyMock version, the second test method that we used with Mockito is the following:
@Test public void joeHas5Friends() { List<String> expected =
Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul"); Person joe = createMock(Person.class); expect(friends.findByName("Joe")).andReturn(joe); expect(joe.getFriends()).andReturn(expected); replay(friends); replay(joe); assertThat(friendships.getFriendsList("Joe")) .hasSize(5)
.containsOnly("Audrey", "Peter", "Michael", "Britney", "Paul"); }
Again, there are almost no differences when compared to Mockito, except that EasyMock does not have spies. Depending on the context, that might be an important difference.
Even though both frameworks are similar, there are small details that make us choose Mockito as a framework, which will be used throughout this book.
Visit http://easymock.org/ for more information about this asserts library.
The complete source code can be found in the FriendshipsMongoEasyMockTest class in the https://bitbucket.org/vfarcic/tdd-java-ch02-example-junit repository.
- 程序設計與實踐(VB.NET)
- 數據結構(Python語言描述)(第2版)
- Java性能權威指南(第2版)
- Learning Probabilistic Graphical Models in R
- Access 2010數據庫應用技術實驗指導與習題選解(第2版)
- Web App Testing Using Knockout.JS
- Go語言從入門到精通
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- Hacking Android
- Three.js權威指南:在網頁上創建3D圖形和動畫的方法與實踐(原書第4版)
- 創新工場講AI課:從知識到實踐
- 一步一步學Spring Boot:微服務項目實戰(第2版)
- Visual FoxPro程序設計習題及實驗指導
- Docker on Windows
- Mastering React Test:Driven Development