I just written sample test case, where I would like to mock a void instance method. I am surprised, my test case is passing without calling the expectLastCall method. I would like to know, is calling of expectLastCall is not required while mocking instance void methods?
StringUtil.java
package com.sample.util;
import com.sample.model.MethodNotImplementedException;
public class StringUtil {
public String toUpperAndRepeatStringTwice(String str) {
String upperCase = str.toUpperCase();
sendStringToLogger(upperCase);
return upperCase + upperCase;
}
public void sendStringToLogger(String str){
throw new MethodNotImplementedException();
}
}
StringUtilTest.java
package com.sample.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({ StringUtil.class })
public class StringUtilTest {
#Test
public void toUpperAndRepeatStringTwice() {
StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, "sendStringToLogger");
String str = "HELLO PTR";
stringUtil.sendStringToLogger(str);
//PowerMock.expectLastCall().times(1);
PowerMock.replayAll();
String result = stringUtil.toUpperAndRepeatStringTwice("hello ptr");
assertEquals(result, "HELLO PTRHELLO PTR");
}
}
expectLastCall is not required. For EasyMock and the PowerMock layer as well. So you are right.
It is used for clarity for some users. Because it makes it obvious that the method before is an expectation not some random call. But it is more a question of style than a requirement.
You also don't need the time(1) since it is the default.
BTW, the answer here is wrong and I've commented on it accordingly.
Related
I'm running some code using Junit4 and an external jar of JunitParams.
As far as I could tell, my code was all set right, and imports are all correct...but my method under test keeps throwing runtime exceptions since it seems to ignore the #Parameters annotation hanging out in it.
When it reaches my method under test, it keeps saying "java.lang.Exception: Method ----- should have no parameters"
I'm not sure what the issue is, even though other people have run into issues with mix/matched import statements, etc. I checked mine and that doesn't seem like the case here.
Test Runner class:
package Chapter8.Wrappers.Tests;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args){
Result result = JUnitCore.runClasses(StringProcessorTest.class);
for(Failure failures: result.getFailures()) {
System.out.println(failures.toString());
}
System.out.println(result.wasSuccessful());
}
}
and my test class, StringProcessorTest :
package Chapter8.Wrappers.Tests;
import Chapter8.Wrappers.Challenges.BackwardsString.StringProcessor;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Arrays;
import java.util.Collection;
#RunWith(JUnitParamsRunner.class)
public class StringProcessorTest {
private StringProcessor processor;
private final String expectedResult;
private final String inputString;
public StringProcessorTest(String inputString, String expectedResult){
super();
this.inputString = inputString;
this.expectedResult = expectedResult;
}
#Before
public void initialize(){
processor = new StringProcessor();
}
#Parameters
public static Collection stringValues(){
return Arrays.asList(new Object[][] { {"HELLO","OLLEH"},
{"TESTING", "GNITSET"} });
}
#Test
public void objectConstructor_InitializeStringBuilderInside_EqualsPassedString(String inputString, String expectedResult){
Object input;
input = inputString;
processor = new StringProcessor(input);
Assert.assertEquals(expectedResult, processor.printReverseString());
}
}
Console output:
Chapter8.Wrappers.Tests.StringProcessorTest: java.lang.Exception: Method objectConstructor_InitializeStringBuilderInside_EqualsPassedString should have no parameters
false
Process finished with exit code 0
I found something that ended up working out- I changed the inside of the #RunWith annotation to
Parameterized.class
It turned out I was trying to pass parameters into the method under the #Test annotation, and not just the constructor of my StringProcessorTest class; I removed them from the method under test. It was already warning me about that but I didn't understand why earlier.
When I re-ran it, it completed without issues and ran both times.
I think you have two choices to resolve your issue:
Remove the #Parameters annotation from stringValues() method and add it to your test method like this:
private List stringValues(){
return Arrays.asList(new Object[][] { {"HELLO","OLLEH"}, {"TESTING", "GNITSET"} });
}
#Test
#Parameters(method = "stringValues")
public void objectConstructor_InitializeStringBuilderInside_EqualsPassedString(String inputString, String expectedResult) {
processor = new StringProcessor(inputString);
Assert.assertEquals(expectedResult, processor.printReverseString());
}
OR
Remove stringValues() method and put the #Parameters annotation directly on your test method:
#Test
#Parameters({ "HELLO, OLLEH", "TESTING, GNITSET" })
public void objectConstructor_InitializeStringBuilderInside_EqualsPassedString(String inputString, String expectedResult) {
processor = new StringProcessor(inputString);
Assert.assertEquals(expectedResult, processor.printReverseString());
}
I'm trying to test a class which uses a calculator class with a number of static methods. I've successfully mocked another class in a similar way, but this one is proving more stubborn.
It seems that if the mocked method contains a method call on one of the passed in arguments the static method is not mocked (and the test breaks). Removing the internal call is clearly not an option. Is there something obvious I'm missing here?
Here's a condensed version which behaves the same way...
public class SmallCalculator {
public static int getLength(String string){
int length = 0;
//length = string.length(); // Uncomment this line and the mocking no longer works...
return length;
}
}
And here's the test...
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.solveit.aps.transport.model.impl.SmallCalculator;
#RunWith(PowerMockRunner.class)
#PrepareForTest({ SmallCalculator.class})
public class SmallTester {
#Test
public void smallTest(){
PowerMockito.spy(SmallCalculator.class);
given(SmallCalculator.getLength(any(String.class))).willReturn(5);
assertEquals(5, SmallCalculator.getLength(""));
}
}
It seems there's some confusion about the question, so I've contrived a more 'realistic' example. This one adds a level of indirection, so that it doesn't appear that I'm testing the mocked method directly. The SmallCalculator class is unchanged:
public class BigCalculator {
public int getLength(){
int length = SmallCalculator.getLength("random string");
// ... other logic
return length;
}
public static void main(String... args){
new BigCalculator();
}
}
And here's the new test class...
import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.solveit.aps.transport.model.impl.BigCalculator;
import com.solveit.aps.transport.model.impl.SmallCalculator;
#RunWith(PowerMockRunner.class)
#PrepareForTest({ SmallCalculator.class})
public class BigTester {
#Test
public void bigTest(){
PowerMockito.spy(SmallCalculator.class);
given(SmallCalculator.getLength(any(String.class))).willReturn(5);
BigCalculator bigCalculator = new BigCalculator();
assertEquals(5, bigCalculator.getLength());
}
}
I've found the answer here https://blog.codecentric.de/en/2011/11/testing-and-mocking-of-static-methods-in-java/
Here's the final code which works. I've tested this approach in the original code (as well as the contrived example) and it works great. Simples...
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({ SmallCalculator.class})
public class BigTester {
#Test
public void bigTest(){
PowerMockito.mockStatic(SmallCalculator.class);
PowerMockito.when(SmallCalculator.getLength(any(String.class))).thenReturn(5);
BigCalculator bigCalculator = new BigCalculator();
assertEquals(5, bigCalculator.getLength());
}
}
First of all, remove that line:
given(SmallCalculator.getLength(any(String.class))).willReturn(5);
Since you are testing the same method. You don't want to mock the method that you're testing.
Secondly, modify your annotation to:
#PrepareForTest({ SmallCalculator.class, String.class})
Lastly, add the mock for the length(); like this:
given(String.length()).willReturn(5);
I think it will do ;)
Use anyString() instead of any(String.class).
When using any(String.class) the passed argument is null, as Mockito will return the default value for the reference type, which is null. As a result you get an exception.
When using the anyString(), the passed argument will be empty string.
Note, this explains why you get an exception, however you need to review the way you test your method, as explained in other comments and answers.
If you do not want that actual method being invoked. Instead of
when(myMethodcall()).thenReturn(myResult);
use
doReturn(myResult).when(myMethodCall());
It is mocking magic and it is difficult to explain why it is actually works.
Other thing you forgot is mockStatic(SmallCalculator.class)
And you do not need PowerMockito.spy(SmallCalculator.class);
I was trying out PowerMock, and am trying to mock a private method like so:
expectPrivate(n, "doLogin", anyString(), anyString()).andReturn(true);
That is, I want true to be returned from doLogin irrespective of the parameters passed.
The public method which delegates to this private method simply passes-on the arguments. Here is the definition of the class to be mocked:
class N {
public boolean login(String username, String password) {
return doLogin(username, password);
}
private boolean doLogin(String u, String p){
//validate login
//...
//...
return true;
}
}
And this is the test class where I am trying to invoke the mock:
import static org.junit.Assert.assertEquals;
import static org.powermock.api.easymock.PowerMock.createPartialMock;
import static org.powermock.api.easymock.PowerMock.expectPrivate;
import static org.powermock.api.easymock.PowerMock.replay;
import static org.powermock.api.easymock.PowerMock.verify;
import static org.mockito.Matchers.anyString;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
#RunWith(PowerMockRunner.class)
#PrepareForTest(N.class)
public class NodeAccessorTest {
private String username = "admin";
private String password = "asdf";
#Test
public void testMockLogin() throws Exception {
N n = createPartialMock(N.class,
"doLogin", String.class, String.class);
boolean expected = true;
expectPrivate(n, "doLogin", anyString(), anyString()).andReturn(expected);
replay(n);
boolean actual = n.login("A", "B");
verify(n);
assertEquals("Expected and actual did not match", expected, actual);
}
}
This is the failure trace:
java.lang.AssertionError:
Unexpected method call N.doLogin("A", "B"):
N.doLogin("", ""): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:91)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:124)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:185)
at com.pugmarx.mock.N.doLogin(N.java)
at com.pugmarx.mock.N.login(N.java:60)
So the mocking framework is not happy when specific Strings are passed to the public login() method, but fine when anyString is used. Ideally, I would expect that since the call to the private doLogin is mocked, this should not be the case.
What am I missing?
I had a similar issue and I think your problem can be in the Matchers anyString() passed by param to PowerMock expectPrivate method.
Based in your imports, you are using Mockito Matchers instead EasyMock Matchers: org.mockito.Matchers.anyString.
Could you try to changing the Matchers for the next: EasyMock.anyString()
import org.easymock.EasyMock;
...
expectPrivate(n, "doLogin", EasyMock.anyString(), EasyMock.anyString()).andReturn(expected);
Hope it helps.
How do I set an expectation on a final method if I can't safely invoke that method at all? PowerMock is supposed to ensure the invocation is mocked, but I can't even get to that stage:
WithFinal.java:
public class WithFinal {
public final void finalMethod() {
throw new RuntimeException();
}
}
CallsFinal.java:
public class CallsFinal {
private WithFinal withFinal;
public CallsFinal(WithFinal withFinal) {
this.withFinal = withFinal;
}
public void callFinal() {
withFinal.finalMethod();
}
}
PowerMockTest.java:
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.powermock.api.easymock.PowerMock.*;
#RunWith(PowerMockRunner.class)
#PrepareForTest(CallsFinal.class)
public class PowerMockTest {
#Test public void testFinal() {
WithFinal mock = createMock(WithFinal.class);
CallsFinal callsFinal = new CallsFinal(mock);
mock.finalMethod();
EasyMock.expectLastCall().atLeastOnce();
replay(mock);
callsFinal.callFinal();
verify(mock);
}
}
I get a RuntimeException on the very first call to mock.finalMethod(), which makes sense, but I thought the whole point of PowerMock was to make this possible?
There was a simple mistake in the test class: instead of #PrepareForTest(CallsFinal.class), it should have been #PrepareForTest(WithFinal.class).
PowerMock only requires that the calling class be prepared for test when mocking a system class from the JRE; otherwise, it's the class to be mocked itself that needs to get prepared.
Finally, I will mention there is another mocking library that can be used here, which I happen to develop: JMockit. With it, the test can be written as:
import org.junit.*;
import mockit.*;
public class JMockitTest {
#Tested CallsFinal callsFinal;
#Injectable WithFinal mock;
#Test public void testFinal() {
new Expectations() {{ mock.finalMethod(); }};
callsFinal.callFinal();
}
}
Using PowerMock, you can mock skip a internal method call instead of direct method call.
For example you want to test callFinal method of CallsFinal class which internally calling finalMethod of WithFinal class. So in this case if you don't want to instantiate WithFinal class then you need to mock WithFinal object to skip internal call for finalMethod.
PowerMock provides the method expectPrivate to mock out private methods, however it appears only in EasyMock api and not the Mockito API.
So, is there an equivalent for PowerMockito? I'm guessing not because I haven't found it and because of this wiki entry. but that doesn't actually prevent PowerMockito from working around it. So, I'm asking this mostly for confirmation and since I think this will be of value for others.
PowerMockito provides ways to mock private methods as well, from the API:
<T> WithOrWithoutExpectedArguments<T> when(Object instance, Method method)
Expect calls to private methods.
verifyPrivate(Object object, org.mockito.verification.VerificationMode verificationMode)
Verify a private method invocation with a given verification mode.
There are a bunch of other signatures of the type described above.
An example:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.eq;
#RunWith(PowerMockRunner.class)
#PrepareForTest(Foo.class)
public class SimpleTest {
#Test
public void testHello() throws Exception {
Foo foo = PowerMockito.spy(new Foo());
foo.sayHello();
PowerMockito.verifyPrivate(foo).invoke("hello", eq("User"));
}
}
class Foo {
public void sayHello() {
System.out.println(hello("User"));
}
private String hello(String user) {
return "Hello " + user;
}
}