I have following code line:
when(htmlEmailSpy.setFrom(anyString())).thenReturn(null);
Following code executes real htmlEmailSpy.setFrom(...) but it throws exception.
#Spy
HtmlEmail htmlEmailSpy = new HtmlEmail();
What do I wrong?
my aim - set new behaviour to spy object.
I have resolved problem:
doReturn(null).when(htmlEmailSpy).setFrom(anyString());
took from: Mockito: Trying to spy on method is calling the original method
Related
I am trying to mock class instance and then return the mock object when we call the instance function but in result, it makes an actual call to function.
Here is my code:
AuthenticationSessionModel authSessionCookie =
new AuthenticationSessionManager(session)
.getCurrentAuthenticationSession(realm, client, tabId);
and my test code is:
AuthenticationSessionManager spyAuthSessionManager =
Mockito.spy(new AuthenticationSessionManager(session));
doReturn(authenticationSessionModel)
.when(spyAuthSessionManager)
.getCurrentAuthenticationSession(any(), any(), anyString());
It makes an actual call to getCurrentAuthenticationSession() and returns me Null Pointer Exception
In the test, you create a spy, and stub some behaviour.
But you don't use that spy in the code under test.
Instead, in the code under test you create a new AuthenticationSessionManager.
You need to restructure your code and:
create AuthenticationSessionManager outside of object under test.
pass it to object under test. Constructor is the first thing that comes to mind.
With these changes, it becomes trivial to substitute a real AuthenticationSessionManager with a spy in a test.
I have a situation where first argument of method findUser is something I cant mock Session.get(), So it will throw exception but still I want mock findUser. Any clue guys how to do this. I am using Mockito here.
eg.
Emp emp = findUser(Session.get(), "Admin");
Note Where Session isn't a static class. It is a static variable of the same class.
There is a library called PowerMockito --> https://www.baeldung.com/intro-to-powermock . You should be able to mock your Session varg as a mock or spy and handle the return get(). This should then allow you to mock your findUser method passing in the argument types to be expected. Cheers!
I am trying to mock a class with Mockito that has a method which chains an interface. The class is mocked successfully but when it calls the interface a null pointer is thrown. The code looks as below:
mock = Mockito.mock(MyProcess.class);
process = mock.getProcess()
.getService() //Interface throwing null exception
.startProcessInstanceByKey("String argument");
I got this solution and tried to follow the example on the page below but its not working: https://static.javadoc.io/org.mockito/mockito-core/2.13.0/org/mockito/Mockito.html#RETURNS_DEEP_STUBS
Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
// note that we're stubbing a chain of methods here: getBar().getName()
when(mock.getBar().getName()).thenReturn("deep");
// note that we're chaining method calls: getBar().getName()
assertEquals("deep", mock.getBar().getName());
The example above is not working
You need to mock the retuning Service, too. All external dependencies of you classes need to be mocked and if you doesn´t do this you get null.
processMock = Mockito.mock(MyProcess.class);
serviceMock= Mockito.mock(Service.class);
Mockito.doReturn(serviceMock).when(processMock).getService();
Mockito.doReturn(<VALUE>).when(serviceMock).startProcessInstanceByKey("String argument");
You need to mock every step with external values - this is how it works.
I'm trying to mock Lucenes IndexReader.close() to do nothing.
I thought this should work...
IndexReader reader = Mockito.mock(IndexReader.class);
Mockito.stubVoid(reader).toReturn().on().close(); // old approach
Mockito.doNothing().when(reader).close(); // new approach
but both result in the unit test calling the actual, real close method and ultimately causing a null pointer exception.
What have I missed?
As the javadoc indicates, close() is a final method. And Mockito can't mock final methods.
I have a method which does the following as a part of the operation:
URL resourceUrl = new File(sampleString1.concat("/")
.concat(sampleString2)).toURI().toURL();
SampleString1 and SampleString2 are populated by the constructor of the class and the class is an abstract class. I used the below line to test this class:
servlet = PowerMockito.mock(MyServlet.class, Mockito.CALLS_REAL_METHODS);
Through this i'm unable to set values of attributes sampleString1 and sampleString2 as i'm not passing them to constructor. Now, when i call my test method in servlet, i get NullPointerExceptions at the above mentioned line because of null values at sampleString1 and sampleString2. I tried to simulate that part by the following:
PowerMockito.whenNew(File.class).withParameterTypes(String.class).
withArguments(Matchers.anyString()).thenReturn(sampleFile);
but still it goes through the string concatanations when new file call is done and throws exception. How can i test this method? I just want to return a sample value for resourceUrl when new file operation is called and proceed with the logic in the method and test the output.
Thanks.
You can use the Whitebox API documented at the following locations:
http://code.google.com/p/powermock/wiki/BypassEncapsulation
http://powermock.googlecode.com/svn/docs/powermock-1.4.12/apidocs/org/powermock/reflect/Whitebox.html
It uses reflection to set the values on an object like so:
MyServlet servlet = PowerMockito.mock(MyServlet.class, Mockito.CALLS_REAL_METHODS);
Whitebox.setInternalState(servlet, "sampleString1", "foo");
Whitebox.setInternalState(servlet, "sampleString2", "bar");
//Test code...