I want to test my Service class method testB1Method2 by mocking overridden method a1Method2 of class B1. I do not want to change anything in class A1 and B1. I am using mockito 1.9.0 and powermockito 1.4.12. The following code I am trying:
UnitTestService class:
import static org.mockito.Mockito.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.testng.Assert;
import org.testng.annotations.Test;
public class UnitTestService {
#Mock
B1 b1;
#InjectMocks
Service service = new Service();
#Test
public void testB1Method2() throws Exception {
MockitoAnnotations.initMocks(this);
when(b1.a1Method2()).thenReturn("mockvalue");
PowerMockito.whenNew(B1.class).withArguments(Mockito.any()).thenReturn(b1);
String output = service.serviceMethod();
System.out.println("=====" + output);
Assert.assertTrue("mockvalue".equalsIgnoreCase(output), "testA1Method2 failed!");
}
}
Service class:
public class Service {
public String serviceMethod() {
B1 b1 = new B1("some data");
return b1.a1Method2();
}
}
class A1:
public abstract class A1 {
public A1(String data) {
//doing many thing with data
}
public String a1Method1() {
return "from a1Method1";
}
public String a1Method2() {
return "from a1Method2";
}
}
B1 class:
public class B1 extends A1 {
public B1(String data) {
super(data);
}
#Override
public String a1Method1() {
return "a1Method1 from B1 class";
}
}
I am running class UnitTestService using testNG in eclipse. And here actual method in class B1 a1Method2 is getting called as it is printing "=====from a1Method2" in console. ie: here it seems mockito is not able to mock this method.
What code change should I make in UnitTestService class to mock class B1 a1Method2 ?
You created hard to test code there, for absolutely no good reason.
What is breaking your neck is that small little call to new B1 in your service class.
If you would be using dependency injection for that object; then you would absolutely not need to deal with powermock and mocking inherited methods. Because then you could simply create a "mock" B1; pass that to your service; and then you have full control over what will be happening during your test.
So, the viable alternative here might be to avoid complex test setup by simply improving your production code to be easier to test.
Watch those videos to better understand what I am talking about!
Related
I have a Mockito/PowerMockito issue!
The class to test is as below :
public class ClassToTest {
private String string;
public ClassToTest(String s) {
this.string = s;
}
public String concatenate() {
return string.concat(" - Done!");
}
public ClassToTest create(String s) {
return new ClassToTest(s);
}
}
The test class i wrote :
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassToTest.class)
public class ClassToTestTest {
#Test
public void concatenate() throws Exception {
ClassToTest classToTest = Mockito.mock(ClassToTest.class);
PowerMockito.whenNew(ClassToTest.class).withArguments(Mockito.anyString()).thenReturn(classToTest);
classToTest.concatenate();
}
}
Question - How do i set a value of the instance variable named "string" from test class so that i can test concatenate method(concatenate method uses the constructor initialized "string" variable") Currently the debug point is not even going inside concatenate() method. I need to do this with either mockito/powermock.
Note - The example I have given is a representation of the issue i am facing in real time.
Any leads would help me.
Thanks in advance!!
Your test is pointless for several reasons.
you mock the class you're trying to test
you mock a method that is not even called in the test
you don't verify the result of the method you call.
Your test can just be
ClassToTest tested = new ClassToTest("test"); // create instance
String concatResult = tested.concatenate(); // call method under test
assertThat(concatResult).isEqualTo("test - Done"); // verify result
No need to mock anything. If you want to test the create method (of which I don't see the point at all btw), you can do
ClassToTest tested = new ClassToTest(""); // the String is never used anyway
ClassToTest created = tested.create("test"); // call method under test
assertThat(created.concatenate()).isEqualTo("test - Done"); // verify
If you mock classes you're testing, you don't test the classes behavior, but only the mocked result. Consider
Don't do this
ClassToTest mock = mock(ClassToTest.class);
ClassToTest other = mock(ClassToTest.class);
when(mock.create(anyString()).thenReturn(other);
when(other.concatenate(anyString()).thenReturn("expected");
ClassToTest created = mock.create("test");
String result = created.concatenate("lala");
assertThat(result).isEqualTo("expected"); // duh
I have the following structure
public class classAImpl implements classA {
public ClassC getTarget(Classc cObj) {
// library call
RegistryLib.init();
// some more code to construct cObj with more info
return cObj
}
}
// Registry Library Class
Class RegistryLibClass{
public void init() {
ClassD.builder.build();
}
}
My test class is trying to test a method which calls getTarget() method written above. I want to completely avoid executing getTarget() method which I am not been able even after mocking it. So far I have tried the following:
Class Testclass {
#Before
public void setUp() {
Testclass mock = PowerMockito.mock(Testclass.class);
PowerMockito.when(mock.getTarget(cObj)).thenReturn(cObj);
}
private ClassC getTarget(cObj) {
return cObj;
}
}
Any help is appreciated!
Assuming you want to test a method in Class B that calls getTarget from Class A, you would do this,
B b = new B();
A a = Mockito.mock(A.class);
C c = new C();
Mockito.when(a.getTarget(Mockito.any(C.class)).thenReturn(c);
boolean isPresent = b.someMethodToTest();
assertEquals("someMethodToTest was supposed to return true", true/*expected*/, isPresent);
Edit#1
You need to use Powermockito to mock the static method to return nothing as mentioned here
#PrepareForTest(RegistryLibClass.class) //at the top of the class
//inside your test
PowerMockito.mockStatic(RegistryLibClass.class);
Mockito.when(RegistryLibClass.init()).doNothing();
Disclaimer - I'm not that familiar with mockito, but wouldn't you normally mock the class you want to avoid using? Like this:
class Testclass {
#Before
public void setUp() {
//create some mock of expected cObj here to be returned.
classAImpl mock = PowerMockito.mock(classAImpl.class);
PowerMockito.when(
mock.getTarget(cObj.class /*Shouldn't this be class call?*/))
.thenReturn(cObj);
}
}
And then you would inject mock as dependency into the object that uses it and that you want to test.
If I'm wrong then feel free to ignore me, but thats how other mocking libraries I used worked. I'd advise you to go read some tutorials about mocking for tests regardless.
As side note, use of Class and class in names made this example extremely difficult to understand.
Your example is very confusing as you are trying to mock your test class itself instead of the main class.
Also you have mentioned that you are trying to test a method which calls getTarget() method, but I don't see any method in your class that calls getTarget() method.
I have put down a simple example below for your understanding. Let me know if this helps.
ClassA
public class ClassA {
public String method1() {
return "ClassA -> method1";
}
public static String method2() {
return "ClassA -> method2";
}
}
ClassB calls Class A methods
public class ClassB {
public void method1() {
System.out.println("ClassB -> method1");
new ClassA().method1();
ClassA.method2();
}
}
ClassB Test
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassA.class)
public class ClassBTest {
#Mock
ClassA mock;
#Before
public void setUp() throws Exception {
// Initialize mocks
MockitoAnnotations.initMocks(this);
// This is for mocking new objects
PowerMockito.whenNew(ClassA.class).withNoArguments().thenReturn(mock);
PowerMockito.when(mock.method1()).thenReturn("Mocked Method 1");
// This is for mocking static methods
PowerMockito.mockStatic(ClassA.class);
PowerMockito.when(ClassA.method2()).thenReturn("Mocked Method 2");
}
#Test
public void testMeth() {
System.out.println(new ClassA().method1()); // Prints - Mocked Method 1
System.out.println(ClassA.method2()); // Prints - Mocked Method 2
}
}
I want to mock a static method using PowerMockito,
public class DepedencyService {
public static int getImportantValue() {
return -4;
}
}
public class Component {
public int componentMethod() {
return DepedencyService.getImportantValue();
}
}
but it is giving me an exception.
import static org.testng.Assert.assertEquals;
import org.easymock.EasyMock;
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(DepedencyService.class)
public class ComponentTest {
#Test
public void testComponentMethod() {
Component c = new Component();
PowerMockito.mockStatic(DepedencyService.class);
EasyMock.expect(DepedencyService.getImportantValue()).andReturn(1);
assertEquals(1, c.componentMethod());
}
}
The exception :-
java.lang.IllegalStateException: no last call on a mock available at
org.easymock.EasyMock.getControlForLastCall(EasyMock.java:520) at
org.easymock.EasyMock.expect(EasyMock.java:498)
Can anyone please help me? Why is this failing? I am new to PowerMockito and does not know what to do here!
Your main problem is that you're writing STUPID code (like most of us did at the beginning) where you rather should write SOLID code.
Using Powermock is just a surrender to this bad design.
Yes, classes having only static methods are called utility classes.
But you should get over this misconception that classes providing common behavior should have (only) static methods.
As a rule of thumb there should be only one non private static method in your entire program, and this is main().
You appear to be mixing mocking frameworks.
You need to properly arrange the static dependencies before exercising the test
Since PowerMockito is used to mock the static class then you should use Mockito to arrange the expected behavior
For example
#RunWith(PowerMockRunner.class)
#PrepareForTest(DepedencyService.class)
public class ComponentTest {
#Test
public void testComponentMethod() {
//Arrange
int expected = 1;
PowerMockito.mockStatic(DepedencyService.class);
Mockito.when(DepedencyService.getImportantValue()).thenReturn(expected);
Component subject = new Component();
//Act
int actual = subject.componentMethod();
//Assert
assertEquals(expected, actual);
}
}
That said, I would advise not having your code tightly coupled to static dependencies. It makes for difficult to test code.
I am trying to Unit Test a class in Java.
Code for this class: ToBeTested
public class ToBeTested {
private Collaborator collaborator;
public ToBeTested() {
System.out.println("ToBeTested: Constructor");
}
public void start() {
System.out.println("ToBeTested: Start");
collaborator = new Collaborator();
}
}
This class ToBeTested depends on another class, Collaborator.
Code for class: Collaborator
public class Collaborator {
Collaborator() {
System.out.println("Collaborator: Constructor");
}
}
While testing the class ToBeTested, I want to stub instantiation of Collaborator. That's a dependency I want to mock and I don't want it's constructor to be called.
I'm using Junit (v4.12) and PowerMock (v1.6.1).
Code for Test Class: TestToBeTested
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.easymock.PowerMock.*;
#RunWith(PowerMockRunner.class)
#PrepareForTest({ToBeTested.class, Collaborator.class})
public class TestToBeTested {
#Mock
private Collaborator collaborator;
private ToBeTested toBeTested;
#Before
public void setUp() throws Exception {
collaborator = createMock(Collaborator.class);
expectNew(collaborator.getClass()).andReturn(null);
toBeTested = new ToBeTested();
}
#Test
public void test() {
replayAll();
toBeTested.start();
verifyAll();
}
}
My understanding is that this will mock or stub out Collaborator and it's constructor should not be called. However, when I run the test, I notice that original constructor of Collaborator is called.
Output of test run:
ToBeTested: Constructor
ToBeTested: Start
Collaborator: Constructor
I'm very new to Java and Unit Testing in Java, so I apologize if I'm doing a very fundamental mistake here.
During my quest to find out the root cause, I have referred to following SO questions:
PowerMock's expectNew() isn't mocking a constructor as expected
PowerMock expectNew how to specify the type of the parameters
Not able to mock constructor using PowerMock
https://dzone.com/articles/using-powermock-mock
Thank you very much in advance for help/suggestions/feedback.
One possible reason that it might not be working could be this line:
expectNew(collaborator.getClass()).andReturn(null);
collaborator is a mocked instance which means it's "getClass()" method is going to return Collaborator$CGLIBMockedWithPowermock or something like that -- not the Collaborator class you want it to be. So you might get it to work simply by changing that line to:
expectNew(Collaborator.class).andReturn(null);
Suppose I have a class like so:
public class StaticDude{
public static Object getGroove() {
// ... some complex logic which returns an object
};
}
How do I mock the static method call using easy mock? StaticDude.getGroove().
I am using easy mock 3.0
Not sure how to with pure EasyMock, but consider using the PowerMock extensions to EasyMock.
It has a lot of cool functions for doing just what you need -
https://github.com/jayway/powermock/wiki/MockStatic
Easymock is a testing framework for "for interfaces (and objects through the class extension)" so you can mock a class without an interface. Consider creating an interfaced object with an accessor for your static class and then mock that acessor instead.
EDIT: Btw, I wouldn't recommend doing static classes. It is better to have everything interfaced if you are doing TDD.
Just in Case PowerMock is unavailable for any reason:
You could move the static call to a method, override this method in the instantiation of the tested class in the test class, create a local interface in the test class and use its method in the overidden method:
private interface IMocker
{
boolean doSomething();
}
IMocker imocker = EasyMock.createMock(IMocker.class);
...
#Override
void doSomething()
{
imocker.doSomething();
}
...
EasyMock.expect(imocker.doSomething()).andReturn(true);
Generally speaking, it is not possible to mock a static method without using some sort of accessor, which seems to defeat the purpose of using a static method. It can be quite frustrating.
There is one tool that I know of called "TypeMock Isolator" which uses some sort of Satanic Magic to mock static methods, but that tool is quite expensive.
The problem is, I know of no way to override a static method. You can't declare it virtual. you can't include it in an interface.
Sorry to be a negative nelly.
Adding an exemple on how to implements static mock along regular mock of injected classes with EasyMock / PowerMock, since the linked exemple only shows static mock.
And with the PowerMockRunner the #Mock services are not wired on the #TestSubject service to test.
Let say we have a service we want to test, ServiceOne :
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class ServiceOne {
#Autowired
private ServiceTwo serviceTwo;
public String methodToTest() {
String returnServ2 = serviceTwo.methodToMock();
return ServiceUtils.addPlus(returnServ2);
}
}
Which calls another service that we will want to mock, ServiceTwo :
import org.springframework.stereotype.Service;
#Service
public class ServiceTwo {
public String methodToMock() {
return "ServiceTwoReturn";
}
}
And which calls a final class static method, ServiceUtils :
public final class ServiceUtils {
public static String addPlus(String pParam) {
return "+" + pParam;
}
}
When calling ServiceOne.methodToTest() we get "+ServiceTwoReturn" as a return.
Junit Test with EasyMock, mocking only the injected ServiceTwo Spring service :
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import org.easymock.EasyMockRunner;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Test;
import org.junit.runner.RunWith;
#RunWith(EasyMockRunner.class)
public class ExempleTest {
#TestSubject
private ServiceOne serviceToTest = new ServiceOne();
#Mock
private ServiceTwo serviceMocked;
#Test
public void testMethodToTest() {
String mockedReturn = "return2";
expect(serviceMocked.methodToMock()).andReturn(mockedReturn);
replay(serviceMocked);
String result = serviceToTest.methodToTest();
verify(serviceMocked);
assertEquals("+" + mockedReturn, result);
}
}
Junit Test with EasyMock & PowerMock, mocking the injected ServiceTwo Spring service but also the final class and its Static method :
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.reflect.Whitebox.setInternalState;
import org.easymock.Mock;
import org.junit.Before;
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(ServiceUtils.class)
public class ExempleTest {
private ServiceOne serviceToTest;
private ServiceTwo serviceMocked;
#Before
public void setUp() {
serviceToTest = new ServiceOne();
serviceMocked = createMock(ServiceTwo.class);
// This will wire the serviced mocked into the service to test
setInternalState(serviceToTest, serviceMocked);
mockStatic(ServiceUtils.class);
}
#Test
public void testMethodToTest() {
String mockedReturn = "return2";
String mockedStaticReturn = "returnStatic";
expect(serviceMocked.methodToMock()).andReturn(mockedReturn);
expect(ServiceUtils.addPlus(mockedReturn)).andReturn(mockedStaticReturn);
PowerMock.replayAll();
String result = serviceToTest.methodToTest();
PowerMock.verifyAll();
assertEquals(mockedStaticReturn, result);
}
}