I have faced problem of testing Java Services that used Session Scoped objects (Same would by with f.e. Request Scope). My Scoped object was treated as a data, but contained simple operations managing its state. My service did some operation on this object changing it state. First I tried to mock this object and mocking its methods to verify correct behavior. But it was not easy due to complicated structure of mocking object. Then I decided to use a Spy object, but the only reason is to inject this object into my Service with annotation @InjectMocks. I did not change my object at all in the Spy, but used it just for dependency injection, as I did not want to have a setter method only for testing.
So the conclusion is: In Mockito Session/Request Scope Objects can be easily tested when defined as Spy objects injected by @InjectMocks
Showing posts with label Mockito. Show all posts
Showing posts with label Mockito. Show all posts
Monday, 4 February 2013
Sunday, 4 November 2012
Defining Mockito mock bean from Spring xml definition
You can define a spring bean in spring xml context that will be a Mockito mock of a given type:
But if you want to inject that bean by type, you have to define it as a Proxy of given interface:
com.example.Repository
Tuesday, 31 July 2012
ArgumentCaptor in Mockito
Use it to capture argument values for further assertions.
It should be used during verifications. It is very useful when assert on argument values to complete verification.
It can also be fined through annotation: @Captor
More info here: http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentCaptor.html
and : http://docs.mockito.googlecode.com/hg/org/mockito/Captor.html
It should be used during verifications. It is very useful when assert on argument values to complete verification.
It can also be fined through annotation: @Captor
More info here: http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentCaptor.html
and : http://docs.mockito.googlecode.com/hg/org/mockito/Captor.html
Mockito Matchers
When using Mockito you can use MockitoMatchers. You can use predefined ones or create own implementation.
F.e.
F.e.
assertThat(object, CoreMatchers.equalTo(0));More examples as well as own implementations here: http://docs.mockito.googlecode.com/hg/org/mockito/Matchers.html
Monday, 30 July 2012
@InjectMocks - Mockito
It is an annotation used in Mockito on the variable definition telling Mockito to try to inject by type already defined mocks or spies on the annotated object
f.e.:
f.e.:
@InjectMocks private MyService service = new MyService();
More info here: http://docs.mockito.googlecode.com/hg/org/mockito/InjectMocks.html
Subscribe to:
Posts (Atom)