Mockito throw exception on void method


Introduction

Mockito is a java library that helps you to mock objects in your code. Mocking allows you to simulate the behaviour of a real object without actually having to create that object. This is useful when you want to test code that depends on objects that are not yet available or when you want to avoid certain side effects of a real object.

Mockito provides two main features:

  1. The ability to create mock objects
  2. The ability to stub methods on those mock objects

You can use Mockito to create mock objects and stub methods on those mock objects. You can also use Mockito to verify that certain methods were called on your mock objects.

What is Mockito?


Mockito is a mock object framework for Java. It is designed to allow the easy creation of test double objects (mocks) in automated tests.

Mockito can be used to test interaction between different objects, or to mock the behavior of an object when it is not possible or practical to use the real object.

Setting up Mockito


One of the benefits of using Mockito is that it allows you to test the behavior of your code, rather than its implementation. This means that you can write tests for your code without having to rely on mocking external dependencies.

To use Mockito in your project, you need to add the following dependency to your build.gradle file:

testCompile ‘org.mockito:mockito-core:2.+’

In addition to the core dependencies, you may also want to include the following optional dependencies:

testCompile ‘org.mockito:mockito-android:2.+’

// If you are usingGradle, you can also use the DSL:
testImplementation 'org.mockito:mockito-core:2.+'</p><br /><h2>Creating a Mockito Test</h2><br /><p>

Creating a Mockito Test
In this section, we’ll see how to unit test code that uses the Mockito framework. We’ll focus on testing code that calls void methods.

We’ll start by creating a simple class with a void method that we want to test:

public class MyClass {
public void doSomething() {
// do something!
}
}

Next, we’ll create a test case for this class. We’ll create a mock object of the MyClass class using the Mockito framework:

@Test
public void testMyClass() {
MyClass myClass = Mockito.mock(MyClass.class);

// call the doSomething() method on our mock object:
myClass.doSomething();

// verify that the doSomething() method was called:
Mockito.verify(myClass).doSomething(); // successful verification!

                                       // or: Assert.assertTrue(Mockito.verify(myClass).doSomething());                                }   }

If the doSomething() method is not called, then the test will fail with an error message like this: org.mockito.exceptions.misusing.UnstubbedMethodException: when() requires an argument which has to be ‘public’. This instance of MyClass is a private inner class insideAnotherPublicClass and cannot be Stubbed or verified.(MyClass is @InjectMocks field in AnotherPublicClass) Please make sure public methods are stubbed or verified, not private and non-public ones.(Also, if you intend to verify interactions with a mock field, make it static)! at com…. … When you see this error message, it means that you are trying to stub/verify a private or non-public method on your mock object . In order to fix this, you need to make the doSomething() method public . You can also avoid this error by making your mock field static .

Asserting Exceptions with Mockito

Mockito provides an easy way to test for exceptions. To do so, you simply use the Mockito.verify() method and pass in the exception that you expect to be thrown as well as the method that you expect to throw it. For example:

Mockito.verify(myMockedObject).someMethod(“some arg”);

This will verify that someMethod() was indeed called with “some arg” as its argument, and that it throws an IllegalArgumentException when called with that argument.

Conclusion

We have seen that it is possible to mock void methods with Mockito. We can do this by using the Mockito.when(…) method to stub out the void method call, and then calling the verify(…) method to verify that the method was called. We can also use Mockito to throw an exception when a void method is called, by using the Mockito.doThrow(…) method.


Leave a Reply

Your email address will not be published. Required fields are marked *