您的位置:首页 > 其它

Unit tests with Mockito - Tutorial

2015-04-23 17:40 786 查看
Unit tests with Mockito - Tutorial explains testing with the Mockito framework from within the Eclipse IDE.

关键点:

Why use test doublels?

A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. The achievement of this desired goal is typical complicated by the fact that Java classes usually depend on other classes.

To solve this, you can use test doubles.

Some type of Test doubles:

dummy: A dummy object is passed around but never used, i.e., its methods are never called. Such an object can for example be used to fill the parameter list of a method.

Fake: Fake objects have
working implementations, but are usually simplified, for example they use an in memory database and not a real database.

Stub: A stub class is anpartial implementation
for an interface or class withthe purpose of using an instance of this stub class during testing. Stubs usually do responding at all to anything outside what's programmed in for the test. Stubs may also record information
about calls.

Mock: A mock object is adummy implementation
for an interface or a class in whichyou define the output of certain method calls. (Mock objects typically require less code to configure and should therefore be preferred. 下面接着讲的就是生成Mock objects的框架)

Mock object generation

One way: create mock objects manually (via code)
Another way: use a mock framework.

Mock frameworks allow you to create mock objects at runtime and define their behavior.
Mock frameworks also allow to test the expected interaction with the mock object, e.g., can test which methods have been called

Mock frameworks

Popular mock frameworks are EasyMock, jMock and Mockito.

Mockito

Mockito is a popular mock framework which can be used in conjunction with JUnit.
Create mock objects

Mockito supports the creation of mock objects with the static
mock()
method call.
It also supports the creation of mock objects based on the
@Mock
annotation. If you use annotations, annotate your class with the
@RunWith(MockitoJUnitRunner.class)
annotation to use the Mockito test runner. For example:
@Mock

MyDatabase databaseMock;

configure mock objects

when(....).thenReturn(....) can be used to configure mock object by specifing a condition and a return value for this condition. For example:

MyClass test = Mockito.mock(MyClass.class);
test.when(test.getUniqueId()).thenReturn(43); //这里对mock对象test配置了特定行为,即当它的getUniqueId方法被调用时,则返回43.

Verify the calls on the mock objects

verify() method can be used to verify that the specified conditions are met, i.e., that a method has been called with certain parameters. For example:

// check if method testing was called with the parameter 12

Mockito.verify(test).testing(Matchers.eq(12));

Limitations of Mockito

It can not test the following constructs:

final classes
anonymous classes
primitive types

document of Mockito
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: