In a class I'm testing, there is a private method that instantiates the GetRequest class in Unirest. How do I use Mockito so that the instantiation of GetRequest class in getResponse() results in a mock object that I use in my JUnit test?
public ClassUnderTest {
public String methodToTest() {
String url = "http://localhost:8080";
String result = getResponse(url);
return result;
}
private String getResponse(String url) {
GetRequest getRequest = new GetRequest(HttpMethod.GET, url);
... = getRequest.header(...).headers(...).asJson();
...etc...
}
}
Thank you,
Rico
One solution is to use a partial mock as in Use Mockito to mock some methods but not others. I can refactor the call to the constructor in a private method and mock that.
Related
I have read some post about it but nothing solved my problem. I have a class which is singleton and one method of this class is being called inside another class. I need to mock this method call.
Class SingletonClass
{
public static SingletonClass instance()
{
......
return instance;
}
public boolean methodToBeMocked(Object obj)
{
return false;
}
}
And the another class is :
Class A
{
Object doSomeStuff()
{
......
boolean result = SingletonClass.instance.methodToBeMocked();
}
}
And I am mocking the method methodToBeMocked in my test class. I have tried to use doReturn instead of thenReturn as it is suggested in other posts but it did not help.
My test class is :
Class TestClass{
Class A a = new A();
public void test()
{
SingletonClass singletonClass = mock(SingletonClass.class);
doReturn(true).when(singletonClass).methodToBeMocked(any());
a.doSomeStuff(); // here mocked method returns false
// but if I do this below it returns true !!!!
Object obj = new Object();
boolean result = singletonClass.mockedMethod(obj);
}
}
So why I am not getting true when a.doSomeStuff is called ? What is wrong here ?
For the benefit of others, I was using the following mock with the expectation it would not call someMock.someMethod(), unlike the when(someMock.someMethod()).doReturn("someString") usage.
Mockito.doReturn("someString").when(someMock).someMethod();
I could not understand why the real someMethod() was still being called. It turns out the method was specified as final. Mockito can't mock static or final methods.
The problem is the static method public static SingletonClass instance(). The standard mockito library does not support mocking of static methods. I seen two solutions.
You can rewrite small your code as:
Add new method getSingletonClassInstance() to be mocked in test
Class A {
Object doSomeStuff()
{
......
boolean result = getSingletonClassInstance();
}
SingletonClass getSingletonClassInstance(){
return SingletonClass.instance.methodToBeMocked();
}
}
use spy from mockito library to create an instance of Class A
import static org.mockito.Mockito.spy;
.....
Class TestClass{
public void test()
{
Class A a = spy(new A());
SingletonClass singletonClass = mock(SingletonClass.class);
doReturn(true).when(singletonClass).methodToBeMocked(any());
doReturn(singletonClass).when(a).getSingletonClassInstance();
a.doSomeStuff(); // here mocked method returns false
// but if I do this below it returns true !!!!
Object obj = new Object();
boolean result = singletonClass.mockedMethod(obj);
}
}
More information about the spy in mockito. Spy used real instance and invoke real method but provide a functionality to mock specific method. Don't worry about the others method they will continue to work with real implementation, only mocked method will be affected.
You can use power mockito to mock the public static SingletonClass
instance()
I need to know how can I mock a method which uses .collect(java 8) method, below is the method
//return data
public String getData(List<Node> nodes)
{
return nodes.stream().map(node->
getService().compare(new Reference()).collect(Collectors.joining(~));
}
protected getService()
{
return service;
}
I can mock service like
#Mock //mocking service
Service service
now how can I mock
getService().compare(new Reference()).collect(Collectors.joining(~));
Compare method returns CompareRef object. I can use PowerMock.
In your case I would recommend to mock the compare() method, not collect().
Because you work with a stream you may have some nodes. To emulate the behaviour with multiply call of compare() with the same Reference object you may try this variant:
final CompareRef expectedCompareRef1 = new CompareRef();
final CompareRef expectedCompareRef2 = new CompareRef();
final CompareRef expectedCompareRef3 = new CompareRef();
when(service.compare(eq(new Reference())).thenReturn(expectedCompareRef1).thenReturn(expectedCompareRef2).thenReturn(expectedCompareRef3);
then call you getData() method:
final List<Nodes> givenNodes = new ArrayList<>();
givenNodes.add(node1);
givenNodes.add(node2);
givenNodes.add(node3);
final String actualResult = myInstance.getData(givenNodes);
Assert.assertEquals("TODO: expectedResult", actualResult);
As result the stream will collect all your test expectedCompareRefN objects.
Note, to have working eq(new Reference()) your Reference class should implement equals/hashCode methods. Otherwise eq(new Reference()) always be false and thenReturn will not return the specified expected objects.
I need help for below thing,
I have to write a Junit using PowerMock/Mockito for a method which makes a call to a static method of a final class present in an external jar.
The method for which i need to write the JUnit test is:
public class SomeClass {
private PrivateKey privateKeyFromPkcs8(String privateKeyPem) throws IOException {
Reader reader = new StringReader(privateKeyPem);
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
if (section == null) {
throw new IOException("Invalid PKCS8 data.");
}
byte[] bytes = section.getBase64DecodedBytes();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
try {
KeyFactory keyFactory = SecurityUtils.getRsaKeyFactory();
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
} catch (NoSuchAlgorithmException exception) {
} catch (InvalidKeySpecException exception) {
}
throw new IOException("Unexpected exception reading PKCS data");
}
}
In the above code PemReader is a final class and readFirstSectionAndClose(reader, "PRIVATE KEY") is a static method in PemReader.
I have tried writing the test shown below but Section object(section) is showing as null while debugging. Perhaps the actual code (PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY")) is getting called instead of the mock.
#RunWith(PowerMockRunner.class)
#PrepareForTest({SomeClass.class,PemReader.class})
public class SomeClassTest {
#InjectMocks
SomeClass mockSomeClass;
#Mock
private Reader mockReader;
#Mock
private Section mockSection;
#Test
public void testPrivateKeyFromPkcs8() throws Exception {
PowerMockito.mockStatic(PemReader.class);
Mockito.when(PemReader.readFirstSectionAndClose(mockReader, "PRIVATE KEY")).thenReturn(mockSection);
assertNotNull(mockSomeClass.privateKeyFromPkcs8(dummyPrivateKey));
}
}
Please help me in writing a Junit using powermockito/mockito
You have to prepare the final, static class.
Here's an example using the PowerMock annotations for JUnit:
#RunWith(PowerMockRunner.class)
#PrepareForTest({PemReader.class})
public class PemReaderTest {
#Mock
private Reader mockReader;
#Mock
private Section mockSection;
#Test
public void testMockingStatic() {
PowerMockito.mockStatic(PemReader.class);
Mockito.when(PemReader.readFirstSectionAndClose(mockReader, "PRIVATE KEY")).thenReturn(mockSection);
Assert.assertEquals(mockSection, PemReader.readFirstSectionAndClose(mockReader, "PRIVATE KEY"));
}
}
For completeness, here's the definition of PemReader:
public final class PemReader {
public static Section readFirstSectionAndClose(Reader reader, String key) {
return null;
}
}
The above test passes with the following versions:
JUnit: 4.12
Mockito: 2.7.19
PowerMock: 1.7.0
Update 1: based on your updated question. Your test case will pass (or at least the invocation on PemReader.readFirstSectionAndClose will return something) if you just make this change:
Mockito.when(PemReader.readFirstSectionAndClose(
Mockito.any(Reader.class),
Mockito.eq("PRIVATE KEY"))
).thenReturn(mockSection);
The version of this instruction in your current test case relies on equality matching between the StringReader which your code passes into readFirstSectionAndClose and the mocked Reader which your test case supplies. These are not 'equal' hence the mocked invocation's expectations are not met and your mockSection is not returned.
A few, unrelated, notes:
There is no need to include SomeClass.class in #PrepareForTest, you only need to include the classes which you want to mock in that annotation, since SomeClass is the class you are trying to test there is no mocking required for that class.
Using #InjectMocks to instance SomeClass is a bit odd, since SomeClass has no (mockito provided) mocks to inject into it :) you can replace this declaration with SomeClass someClass = new SomeClass();
In the code you supplied SomeClass.privateKeyFromPkcs8 has private scope so it cannot be tested (or called in any way) from SomeClassTest.
I need to write a test case for constructor of the following class, where there is a void method I need to mock and verify .How to verify that createToken is get called using powermockito ?
public class Mytest{
private Static string token;
public Mytest(){
if (token == null){
createToken();
}else
{
Log.error("log message");
}
}
private void createToken() {
// logic to create token
}
}
Test class
public class TestMytest{
//set token to null
PowerMockito.spy(Mytest.class);
final String token = null;
Whitebox.setInternalState(Mytest.class,
"token", token);
//supress the createToken() method
MemberModifier.suppress(MemberMatcher.method(
Mytest.class, "createToken"));
new Mytest();
**//verify(??????????)**
}
Why do you want to do this verification? What you in fact want to do is to check whether the token has been set? BTW Why is it even static? You want to have each instance of the class the same value of the token?
Either add a getter for the token field or make it package private.
public class Mytest{
final String token;
public Mytest(){
createToken();
}
private void createToken() {
// logic to create token
}
}
and you can test it like this (I don't remember if Matchers have this method but you get the idea):
assertThat(new Mytest().token, Matchers.notNull())
Here is an existing class and its method I am trying to mock:
public class ClassUndertest{
private Object field_private = new Object();
public Object method_public()
{
field_private.method();
method_private();
}
private Object method_private()
{
....
return Object;
}
}
My tests partially mocks ClassUndertest:
ClassUndertest partialmockinstance = PowerMock.createPartialMock(ClassUndertest.class, "method_private");
When I run mock object:
partialmockinstance.method_public();
field_private is not intialized and so test throws null pointer.
Is there anyway to circumvent this issue?
Fields are initialized when a constructor is invoked. I think the default behaviour of PowerMock is to not invoke any constructor.
With looking at the javadoc I would try the following method instead:
ClassUndertest partialmockinstance = PowerMock.createPartialMockAndInvokeDefaultConstructor(ClassUndertest.class, "method_private");