Unit tests with Mockito - Tutorial


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值