Home » Core Java » Mockito » Page 2

Mockito

Initializing Mockito with InitMocks

In the Mockito Tutorial for Beginners, we saw a general overview of the use of Mockito framework for Java unit tests. In this example, we will see how we can use the InitMocks initialization method, for an easier but expressive way of creating the mocks, specially useful when we have several dependencies to mock. For this tutorial, we will use: ...

Read More »

Mockito Tutorial for Beginners

Mocking is a testing technique widely used not only in Java, but in any other object oriented programming language, that consists in exchanging . There are several mocking testing frameworks for Java, but this tutorial will explain how to use Mockito, probably the most popular for Java language. For this tutorial, we will use: Java 1.7.0 Eclipse Mars 2, release ...

Read More »

Spring Test Mock Example

A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to mock spring components using Mockito. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2 ...

Read More »

Mockito Mock Private Method Example with PowerMock

A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to mock a private method. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2 1. ...

Read More »

Mockito Captor Example

A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to use ArgumentCaptor class/ Captor annotation of Mockito. Tools and technologies used in this example are Java 1.8, Eclipse ...

Read More »

Mockito ThenReturn Example

In this example we will learn how to use ‘thenReturn’ method of Mockito. A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2 ...

Read More »

JUnit Mockito Example

1. Introduction Methods and ways to create Unit Test cases have evolve ever since it’s introduction. New tools and API´s are now available and they provide a more advanced scheme on creating and executing JUnit Test cases. Services have become more incubated, so creating integration test cases has been a daunting task for any developer. The introduction of this new ...

Read More »

Mockito mock static method example

In this article, I am going to show you how to mock static methods using Powermockito. PowerMock is an open source mocking library. It extends the existing mocking frameworks, such as EasyMocks and Mockito, to add even more powerful features. One of them being mocking static methods. For more information, read Powermock – Mockito Integration Example. My setup details: I am using Maven – ...

Read More »

Powermock – Mockito Integration Example

Most of the mocking frameworks in Java, including Mockito, cannot mock static methods or final classes. If we come across a situation where we need to test these components, we won’t be able to unless we re-factor the code and make them testable. For example: Making private methods packaged or protected Avoiding static methods But re-factoring at the cost of ...

Read More »

Mockito Verify Example

In this article, I am going to show you an example of Mockito Verify. To test the state, we use assert, likewise, to verify the test interactions, we use Mockito.verify. Below are my setup details: I am using Maven – the build tool Eclipse  as the IDE, version Luna 4.4.1. TestNG  is my testing framework, in case you are new to TestNG, please refer TestNG Maven Project ...

Read More »