You can read more about this neat feature of junit4 here: https://github.com/junit-team/junit4/wiki/Rules. Mockito provides following methods that can be used to mock void methods. We will be testing simple ThrowingService that has two methods: In the following JUnit test we show how to change the behavior of the someVoidMethod(..) method in ThrowingService using Mockito: In the first test we used the Mockito statement doThrow().when().method() to configured someVoidMethod to throw IllegalArgumentException when called with argument 0. Comment . rev2023.3.3.43278. Follow Up: struct sockaddr storage initialization by network format-string. To verify that the exception did happen, assert a false condition within the try block after the statement that throws the exception. Here, we configured an add () method which returns void to throw IllegalStateException when called. How can I fix 'android.os.NetworkOnMainThreadException'? This means we have work with the following methods to mock a void method: doThrow (Throwable) doThrow (Class) doAnswer (Answer) doNothing () doCallRealMethod () This is the class we will be using for the examples. It has a void eat() method which the customer object will call when served with the dish. If you want your method to throw an exception, don't catch it, or catch it and throw a custom exception that wraps the original exception. on Tue, 18 Jan 2022 15:28:31 UTC, and last updated on Tue, 18 Jan 2022 15:28:31 UTC. Is it suspicious or odd to stand by the gate of a GA airport watching the planes? Why did Ukraine abstain from the UNHRC vote on China? How can I mock a void method to throw an exception? Void method is mostly mocked to check if it is called with correct parameters, https://javadoc.io/static/org.mockito/mockito-core/3.3.3/org/mockito/Mockito.html#12, For mocking void method when-then mechanism of mockito does not work because it needs return value, Void methods can be handled using doNothing(), doAnswer(), doThrow() or doCallRealMethod(), For mocked object doNothing is the default behavior for every method. If it throws MyException during the first method call (in the preparation stage) then it should fail the test. Have you written a response to this post? How can I check before my flight that the cloud separation requirements in VFR flight rules are met? Example Step 1 Create an interface called CalculatorService to provide mathematical functions File: CalculatorService.java To learn more, see our tips on writing great answers. How to mock a void static method to throw exception with Powermock? By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. Connect and share knowledge within a single location that is structured and easy to search. @JB Nizet I totally agree with you but however if I write doThrow(new Exception()) instead of doThrow(Exception.class), I have the following error when I launch my test ; Expected exception com.company.project.exception.ElementNotFoundException but got org.mockito.exceptions.base.MockitoException: doThrow(new Exception()).when(object).voidMethod(any()); Thanks for posting this here; if the method returns a value : given(mockedObject.methodReturningAnObject()).willThrow(new Exception()); if the method doesn't return anything : willThrow(new Exception()).given(mockedObject).methodReturningVoid()); Explanation form javadoc : "Stubbing voids requires different approach from {@link Mockito#when(Object)} (or BDDMockito.given)because the compiler does not like void methods inside brackets", Mockito test a void method throws an exception, How to make mock to void methods with mockito, docs.mockito.googlecode.com/hg/latest/org/mockito/, How Intuit democratizes AI development across teams through reusability. WebTry this for stubbing void methods to throw exceptions: EasyMock: // First make the actual call to the void method. I'm trying to make the test that cover the catch exception. How do you assert that a certain exception is thrown in JUnit tests? Here, we configured an add () method which returns void to throw IllegalStateException when called. How to verify that a specific method was not called using Mockito? In evaluateFood(), we stub method dish.eat() to throw NotSoTastyException using doThrow() and when() combination. In order to get you prepared for your Mockito development needs, we have compiled numerous recipes to help you kick-start your projects. cacheWrapper.putInSharedMemory ("key", "value"); EasyMock.expectLastCall ().andThrow (new RuntimeException ()); Check: http://easymock.org/api/org/easymock/internal/MocksControl.html#andVoid-- This cookie is set by GDPR Cookie Consent plugin. Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors. Lets create a simple class with a void method that we will mock in our test classes. WebTry this for stubbing void methods to throw exceptions: EasyMock: // First make the actual call to the void method. Connect and share knowledge within a single location that is structured and easy to search. doThrow (): We can use doThrow () when we want to stub a void method that throws exception. 1 Answer Sorted by: 1 Firstly, your method deleteTableEsiti () never throws any exception. Before I start with my example, a bit about my setup: .lepopup-progress-100 div.lepopup-progress-t1>div{background-color:#e0e0e0;}.lepopup-progress-100 div.lepopup-progress-t1>div>div{background-color:#bd4070;}.lepopup-progress-100 div.lepopup-progress-t1>div>div{color:#ffffff;}.lepopup-progress-100 div.lepopup-progress-t1>label{color:#444444;}.lepopup-form-100, .lepopup-form-100 *, .lepopup-progress-100 {font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element div.lepopup-input div.lepopup-signature-box span i{font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element div.lepopup-input div.lepopup-signature-box,.lepopup-form-100 .lepopup-element div.lepopup-input div.lepopup-multiselect,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='text'],.lepopup-form-100 .lepopup-element div.lepopup-input input[type='email'],.lepopup-form-100 .lepopup-element div.lepopup-input input[type='password'],.lepopup-form-100 .lepopup-element div.lepopup-input select,.lepopup-form-100 .lepopup-element div.lepopup-input select option,.lepopup-form-100 .lepopup-element div.lepopup-input textarea{font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:left;background-color:rgba(255, 255, 255, 0.7);background-image:none;border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form-100 .lepopup-element div.lepopup-input ::placeholder{color:#444444; opacity: 0.9;} .lepopup-form-100 .lepopup-element div.lepopup-input ::-ms-input-placeholder{color:#444444; opacity: 0.9;}.lepopup-form-100 .lepopup-element div.lepopup-input div.lepopup-multiselect::-webkit-scrollbar-thumb{background-color:#cccccc;}.lepopup-form-100 .lepopup-element div.lepopup-input>i.lepopup-icon-left, .lepopup-form-100 .lepopup-element div.lepopup-input>i.lepopup-icon-right{font-size:20px;color:#444444;border-radius:0px;}.lepopup-form-100 .lepopup-element .lepopup-button,.lepopup-form-100 .lepopup-element .lepopup-button:visited{font-size:17px;font-weight:700;font-style:normal;text-decoration:none;text-align:center;background-color:rgba(203, 169, 82, 1);background-image:linear-gradient(to bottom,rgba(255,255,255,.05) 0,rgba(255,255,255,.05) 50%,rgba(0,0,0,.05) 51%,rgba(0,0,0,.05) 100%);border-width:0px;border-style:solid;border-color:transparent;border-radius:0px;box-shadow:none;}.lepopup-form-100 .lepopup-element div.lepopup-input .lepopup-imageselect+label{border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form-100 .lepopup-element div.lepopup-input .lepopup-imageselect+label span.lepopup-imageselect-label{font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl:checked+label:after{background-color:rgba(255, 255, 255, 0.7);}.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-classic+label,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-fa-check+label,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-square+label,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl+label{background-color:rgba(255, 255, 255, 0.7);border-color:#cccccc;color:#444444;}.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-square:checked+label:after{background-color:#444444;}.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl:checked+label,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='checkbox'].lepopup-checkbox-tgl+label:after{background-color:#444444;}.lepopup-form-100 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-classic+label,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-fa-check+label,.lepopup-form-100 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-dot+label{background-color:rgba(255, 255, 255, 0.7);border-color:#cccccc;color:#444444;}.lepopup-form-100 .lepopup-element div.lepopup-input input[type='radio'].lepopup-radio-dot:checked+label:after{background-color:#444444;}.lepopup-form-100 .lepopup-element div.lepopup-input div.lepopup-multiselect>input[type='checkbox']+label:hover{background-color:#bd4070;color:#ffffff;}.lepopup-form-100 .lepopup-element div.lepopup-input div.lepopup-multiselect>input[type='checkbox']:checked+label{background-color:#a93a65;color:#ffffff;}.lepopup-form-100 .lepopup-element input[type='checkbox'].lepopup-tile+label, .lepopup-form-100 .lepopup-element input[type='radio'].lepopup-tile+label {font-size:15px;color:#444444;font-style:normal;text-decoration:none;text-align:center;background-color:#ffffff;background-image:none;border-width:1px;border-style:solid;border-color:#cccccc;border-radius:0px;box-shadow:none;}.lepopup-form-100 .lepopup-element-error{font-size:15px;color:#ffffff;font-style:normal;text-decoration:none;text-align:left;background-color:#d9534f;background-image:none;}.lepopup-form-100 .lepopup-element-2 {background-color:rgba(226,236,250,1);background-image:none;border-width:1px;border-style:solid;border-color:rgba(216,216,216,1);border-radius:3px;box-shadow: 1px 1px 15px -6px #d7e1eb;}.lepopup-form-100 .lepopup-element-3 * {font-family:'Arial','arial';font-size:26px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;}.lepopup-form-100 .lepopup-element-3 {font-family:'Arial','arial';font-size:26px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-100 .lepopup-element-3 .lepopup-element-html-content {min-height:36px;}.lepopup-form-100 .lepopup-element-4 * {font-family:'Arial','arial';font-size:19px;color:#555555;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element-4 {font-family:'Arial','arial';font-size:19px;color:#555555;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-100 .lepopup-element-4 .lepopup-element-html-content {min-height:63px;}.lepopup-form-100 .lepopup-element-5 * {font-family:'Arial','arial';font-size:13px;color:#555555;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element-5 {font-family:'Arial','arial';font-size:13px;color:#555555;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:transparent;border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-100 .lepopup-element-5 .lepopup-element-html-content {min-height:60px;}.lepopup-form-100 .lepopup-element-6 * {font-family:'Arial','arial';font-size:13px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element-6 {font-family:'Arial','arial';font-size:13px;color:#333333;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:transparent;background-image:none;border-width:1px;border-style:none;border-color:rgba(216,216,216,1);border-radius:0px;box-shadow:none;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.lepopup-form-100 .lepopup-element-6 .lepopup-element-html-content {min-height:auto;}.lepopup-form-100 .lepopup-element-0 * {font-size:15px;color:#ffffff;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;}.lepopup-form-100 .lepopup-element-0 {font-size:15px;color:#ffffff;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;background-color:#5cb85c;background-image:none;border-width:0px;border-style:solid;border-color:#ccc;border-radius:5px;box-shadow: 1px 1px 15px -6px #000000;padding-top:40px;padding-right:40px;padding-bottom:40px;padding-left:40px;}.lepopup-form-100 .lepopup-element-0 .lepopup-element-html-content {min-height:160px;}. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Thank you for you comment, it motivates me.. . These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc. It can also throw a number of exceptions so I'd like to test those exceptions being thrown. We can customize the behavior based on the mocks method name or the method arguments which is passed to it. Asking for help, clarification, or responding to other answers. Besides reading them online you may download the eBook in PDF format! doAnswer (): We can use this to perform some operations when a mocked object method is called that is returning void. It lets us check the number of methods invocations. @Test public void testxyz() { expectedException. DevPedrada. In the next few sections, I will show you different ways of stubbing the void method eat() to change its behavior. Why are Suriname, Belize, and Guinea-Bissau classified as "Small Island Developing States"? Popularity 9/10 Helpfulness 8/10 Source: stackoverflow.com. It catches it and logs it, but always returns normally. Answer: Here is a java example that uses Mockito to test a method that throws an exception. Now when we call customer.eat(dish), it doesnt throw any exception. Tried to stub CacheWrapper#putInSharedMemory. How do I open modal pop in grid view button? We can't use when ().thenThrow () with void return type, as the compiler doesn't allow void methods inside brackets. when(testingClassObj.testSomeMethod).thenThrow(new CustomException()); Using Junit5, you can assert exception, asserts whether that exception is thrown when testing method is invoked. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. WebVoid method throws an exception Question: Write a java program that uses Mockito on a method that returns a void and throws an exception. Do roots of these polynomials approach the negative of the Euler-Mascheroni constant? In mocking, for every method of mocked object doNothing is the default behavior. We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. WebUse doThrow() when you want to stub the void method to throw exception of specified class.. A new exception instance will be created for each method invocation. Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2. mockito throw exception void method java by DevPedrada on Dec 18 2020 Donate Comment 3 xxxxxxxxxx 1 doThrow(new Exception()).when(mockedObject).methodReturningVoid(); Source: stackoverflow.com Add a Grepper Answer Answers related to mockito void method throw exception throw mockito throw exception void method java by DevPedrada on Dec 18 2020 Donate Comment 3 xxxxxxxxxx 1 doThrow(new Exception()).when(mockedObject).methodReturningVoid(); Source: stackoverflow.com Add a Grepper Answer Answers related to mockito void method throw exception throw Stubbing void methods requires a different approach from when (Object) because the compiler does not like void methods inside brackets. Mockito provides following methods that can be used to mock void methods. document.getElementById( "ak_js_1" ).setAttribute( "value", ( new Date() ).getTime() ); Your email address will not be published. What can a lawyer do if the client wants him to be acquitted of everything despite serious evidence? Methods that return void can't be used with when. Save my name, email, and website in this browser for the next time I comment. By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. To do this we make use of doThrow () method of Mockito class. The approach I'm following is to create a mock for CacheWrapper class, make the methods on CacheWrapper class to throw a RuntimeException, set this mock in an instance of SomeClient and test Someclient#getEntity. WebIf this method fails (e.g. Do new devs get fired if they can't solve a certain bug? The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie We've added a "Necessary cookies only" option to the cookie consent popup. Why do academics stay as adjuncts for years rather than move around? This means we have work with the following methods to mock a void method: doThrow (Throwable) doThrow (Class) doAnswer (Answer) doNothing () doCallRealMethod () This is the class we will be using for the examples. Finally, be aware that you can doCallRealMethod() as well. I have always this error: // Create a CacheWrapper spy and stub its method to throw an exception. I'm not using expected - I know about its issues - that's why I wanted to use catch-exception library but don't know how to with void methods. class); classToTest. doAnswer (): We can use this to perform some operations when a mocked object method is called that is returning void. If you want your method to throw an exception, don't catch it, or catch it and throw a custom exception that wraps the original exception. Mockito provides us with a verify()method that lets us verify whether the mock void method is being called or not. this approach is unacceptable for case when you're testing method of an object that has some state. JUnit 5: How to assert an exception is thrown? Contributed on Dec 18 2020 . I have a method with a void return type. The usual way to stub a non-void method is: But note that eat() doesnt return anything so naturally we wont be able to use the above style of API. Acidity of alcohols and basicity of amines. Let us together figure this out in the following blog using mockito. If you want to test the exception message as well you can use JUnit's ExpectedException with Mockito: If you're using JUnit 4, and Mockito 1.10.x If you ever wondered how to do it using the new BDD style of Mockito: willThrow (new Exception ()).given (mockedObject).methodReturningVoid ()); And for future reference one may need to throw exception and then do nothing: willThrow (new Exception ()).willDoNothing ().given (mockedObject).methodReturningVoid ()); Share How to follow the signal when reading the schematic? 4.2. How can I safely create a directory (possibly including intermediate directories)? mockito throw exception void method java by DevPedrada on Dec 18 2020 Donate Comment 3 xxxxxxxxxx 1 doThrow(new Exception()).when(mockedObject).methodReturningVoid(); Source: stackoverflow.com Add a Grepper Answer Answers related to mockito void method throw exception throw For Example: Mockito. 4 When to use dothrow in a Mockito method? doCallRealMethod ().when (mockDatabaseImpl).updateScores ( anyString (), anyInt ()); We can't use when ().thenThrow () with void return type, as the compiler doesn't allow void methods inside brackets. Mockito.when(myService.doSomething()).thenThrow(new Exception("Cannot process")); then we will have following runtime exception: org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method! doThrow() : We can use doThrow() when we want to stub a void method that throws exception. 1 2 doThrow (new Exception ()).when (mockObject).methodWhichThrowException (); Using Kolmogorov complexity to measure difficulty of problems? In this recipe, we will stub a void method. Mockito provides following methods that can be used to mock void methods. In test eatMultipleDishes(), NotSoTastyException is thrown the first time customer.eat(dish) is called. I have tried lot of ways to do this but none of them work. For this, we'll have to mock the method in such a way that it throws these exceptions. Browse Library. We can stub a void method to throw an exception using doThrow (). Any ideas how I can get the method to throw a specified exception? Please could you expand more about this. Is the God of a monotheism necessarily omnipotent? Mutually exclusive execution using std::atomic? If you want your method to throw an exception, don't catch it, or catch it and throw a custom exception that wraps the original exception. Please read and accept our website Terms and Privacy Policy to post a comment. 4.2. How to follow the signal when reading the schematic? As with many other Java developers, I heavily utilise Mockito as a mocking framework for unit testing. 3. Find centralized, trusted content and collaborate around the technologies you use most. Void method throws an exception | Java code. This cookie is set by GDPR Cookie Consent plugin. How to handle Base64 and binary file content types? Other than that we can also make use of doNothing() and doAnswer() APIs. mockito. Thanks for contributing an answer to Stack Overflow! For void methods, mockito provides a special function called doCallRealMethod () which can be used when you are trying to set up the mock. If the dish is too spicy then the overloaded eat(spice) method is going to throw a RuntimeException. http://easymock.org/api/org/easymock/internal/MocksControl.html#andVoid--, Getting EasyMock mock objects to throw Exceptions, How Intuit democratizes AI development across teams through reusability. We can use one of the options as per requirements. In this class we have a updateName() method. Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. Using Junit5, you can assert exception, asserts whether that exception is thrown when testing method is invoked. Using mockito, you can make the exception happen. WebIt doesn't return a value, so it throws an exception. 2. And you need to test to test that it does throw exception during the second method call, not the first one. In this article, we will show how to configure the method call to throw an exception using Mockito. Let's take an example where we will throw InvalidParamException when updateName() method is called with null id. It lets us check the number of methods invocations. MathApplication makes use of calcService using its add method and the mock throws a RuntimeException whenever calcService.add () method is invoked. Stubbing it with a Unit value to leverage on the strict mode could be done, but it feels quite hacky, the point of strict mode is to avoid repeating yourself It helped me. For example, in test testEatUsingStubVoid(), we stub eat() to simply return without throwing an exception, we can do it using stubVoid() and toReturn(). Different approaches to mock void method? The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". Is it possible to rotate a window 90 degrees if it has the same length and width? First, let's take the case where we want to test whether our class can handle exceptions thrown by the void method. Why did Ukraine abstain from the UNHRC vote on China? doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. Mockito provides us with a verify()method that lets us verify whether the mock void method is being called or not. He works as a principal Engineer in the logistics domain. Here, we shall discuss "How to Mock Void method with Mockito". These cookies track visitors across websites and collect information to provide customized ads. If the dish is not the one customer is expecting then it will throw WrongDishException. Mockito.when(myService.doSomething()).thenThrow(new Exception("Cannot process")); then we will have following runtime exception: org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method! public void deleteCurrentlyLoggedInUser (Principal principal) { if (findLoggedInUser (principal) == null) { throw new UserAlreadyDeletedException (); } userRepository.delete (findLoggedInUser (principal)); } Here is findLoggedInUser: User findLoggedInUser (Principal principal) { return userRepository.findByUsername A place where magic is studied and practiced? Now, if we don't want to simulate the processing of this method, this call itself is sufficient to mock the method. Linear Algebra - Linear transformation question, Styling contours by colour and by line thickness in QGIS, Identify those arcade games from a 1983 Brazilian music video, Acidity of alcohols and basicity of amines. How i can stop call a method void with mockito? The thing is that stubbing a Unit method only makes sense if you wanna make it throw an exception, otherwise the only thing you want out of it is to verify it was called as you mentioned. Browse Library. @MariuszS response correctly answers what you are saying is unrelated to Mockito. WebHere we've added an exception clause to a mock object. Mockito provides following methods that can be used to mock void methods. Why is processing a sorted array faster than processing an unsorted array? How to assert that void method throws Exception using Mockito and catch-exception? Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, How Intuit democratizes AI development across teams through reusability. PowerMockito is a superset (or more of a supplement) that can be used with both these frameworks. By adding another test ( nonExistingUserById_ShouldThrow_IllegalArgumentException ) that uses the faulty input and expects an exception you can see whether your method does what it is supposed to do doThrow() : We can use doThrow() when we want to stub a void method that throws exception. PowerMockito allows you to do things that Mockito or EasyMock don't. doThrow() : We can use doThrow() when we want to stub a void method that throws exception. After our previous blog on difference between thenReturn and thenAnswer mockito methods, we are back with yet another interesting blog on Mockito. All attempts have failed with the same reason: The method when(T) in the type Stubber is not applicable for the arguments (void).