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
It also supports the creation of mock objects based on the
@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
关键点:
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
@Mockannotation. 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
相关文章推荐
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
- Mockito InjectMocks Issue when integrated with Spring Annotation
- Mock Objects in Unit Tests
- Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock
- PHP Unit Testing & Mock Objects with SimpleTest plugin for Eclipse
- unit test use EasyMock:Possible matches are marked with (+1):
- Unit Testing with JUnit - Tutorial
- Unit tests with JUnit
- Mocking Void Methods with Mockito
- org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue
- [翻译]Unit testing with JUnit 4.x and EasyMock in Eclipse - Tutorial
- SPRING + JUNIT + DBUnit + Mockito来做单测
- Parameterized unit tests with JUnit 4
- How to write unit test for CommonsMultipartFile with Mock
- Unit Testing with JUnit - Tutorial
- Adding Unit Tests to a C Project - NetBeans IDE 6.9 Tutorial
- Integrating Visual Studio Team System 2008 Unit Tests with CruiseControl.NET
- Write Unit Tests With VSTeamSystem
- How can I get “Copy to Output Directory” to work with Unit Tests?
- Using Mockito to Unit Test Java Applications