Mockito Iterator Style Stubbing of Consecutive Calls

Sometimes we want to mock different responses for the consecutive calls on the same method. We can create then* methods chain with when() to specify iterator style stubbing in Mockito. Mockito Stubbing Consecutive Calls Let’s….

Mockito Mock Static Method – PowerMock

Mockito allows us to create mock objects. Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock….

Mockito ArgumentCaptor, @Captor Annotation

Mockito ArgumentCaptor is used to capture arguments for mocked methods. ArgumentCaptor is used with Mockito verify() methods to get the arguments passed when any method is called. This way, we can provide additional JUnit assertions….

Mockito @InjectMocks – Mocks Dependency Injection

Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have external dependencies in the class we want to mock. We can specify the mock….

Mockito Argument Matchers – any(), eq()

Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when() and thenReturn() on the mock object. Mockito Argument Matchers – any() Sometimes we….

Mockito Spy – Partial Mock

We can use Mockito Spy to partial mock an object. When we spy on an object, the real methods are being called unless it’s stubbed. Mockito Spy We can create mockito spy objects using two….

Mockito mock examples

Mockito mocking framework provides different ways to mock a class. Let’s look at different methods through which we can mock a class and stub its behaviors. Mockito mock method We can use Mockito class mock()….

Mockito Verify

Mockito Verify methods are used to check that certain behavior happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called. Mockito Verify….

Mockito Mock Void Method

Most of the times Mockito when() method is good enough to mock an object’s behavior. But when we have to mock a void method, we can’t use when(). Mockito Mock Void Method Mockito provides following….

Mockito Stub Exception – JUnit, TestNG

Sometimes our methods throw exceptions and we want to mock the object and test the exceptions. We can use Mockito mock objects with when() and thenThrow() to mock this scenario. Mockito Stub Exception – JUnit….

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages