how do I use powermockito verifyNew? - powermock

Having trouble with this. I've used Powermockito quite a bit in the past. Normally this is pretty smooth. I figured I'd post my problem rather than continue to rummage through examples. So the goal is to verify a call to new for a class. I don't think this is the most popular feature of powermockito.
Here's the test:
import static org.powermock.api.mockito.PowerMockito.verifyNew;
import static org.powermock.api.mockito.PowerMockito.whenNew;
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassUnderTest.class)
public class VerifyNewTest {
ClassUnderTest myClassUnderTest = new ClassUnderTest();
#Before
public void setUp() throws Exception {
}
#Test
public void test() throws Exception {
whenNew(Collaborator.class).withNoArguments().thenReturn(new Collaborator());
myClassUnderTest.doSomething();
verifyNew(Collaborator.class).withNoArguments();
}
}
and said classes
public class ClassUnderTest {
public void doSomething() {
new Collaborator();
}
}
public class Collaborator {
}
My goal was to make this as simple as possible. I suppose I could have added some mock objects and stubbed some behavior. Anyway, I get.
org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.invocationcontrol. MockitoNewInvocationControl.expectSubstitutionLogic(MockitoNewInvocationControl.java:65)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!

Return a mock object in the whenNew() clause would work in your case.
#Test
public void test() throws Exception {
whenNew(Collaborator.class).withNoArguments().thenReturn(mock(Collaborator.class));
myClassUnderTest.doSomething();
verifyNew(Collaborator.class).withNoArguments();
}

Related

mockito unit testing Wanted but not invoked:

I have seen there are similar question already exist in SO , I tried all the solution , but couldn't fix my problem , as I am new to tdd
I have a class like this
public class AppUpdatesPresenter {
public void stopService() {
ServiceManager.on().stopService();
}
}
I have the test class like this
#RunWith(MockitoJUnitRunner.class)
public class AppUpdatesPresenterTest {
#Mock
AppUpdatesPresenter appUpdatesPresenter;
#Mock
ServiceManager serviceManager;
#Mock
Context context;
#Test
public void test_Stop_Service() throws Exception {
appUpdatesPresenter.stopService();
verify(serviceManager,times(1)).stopService();
}
}
When I tried to test that , if I call stopService() method , then ServiceManager.on().stopService(); called at least once .
But I am getting the following error
Wanted but not invoked:
serviceManager.stopService();
-> at io.example.myapp.ui.app_updates.AppUpdatesPresenterTest.test_Stop_Service(AppUpdatesPresenterTest.java:103)
Actually, there were zero interactions with this mock.
Not sure whats gone wrong .
When you call appUpdatesPresenter.stopService();, nothing happened as you didn't tell it what should be happened.
To make your test pass, you need stubbing the appUpdatesPresenter.
#Test
public void test_Stop_Service() throws Exception {
doAnswer { serviceManager.stopService(); }.when(appUpdatesPresenter).stopService()
appUpdatesPresenter.stopService();
verify(serviceManager).stopService();
}
Btw, the above test is meaningless as you stub all the things.
To make the test case meaningful, you should inject the ServiceManager instead of coupling it with AppUpdatePresenter.
public class AppUpdatesPresenter {
private final ServiceManager serviceManager;
public AppUpdatesPresenter(ServiceManager serviceManager) {
this.serviceManager = serviceManager;
}
public void stopService() {
sm.stopService();
}
}
Then make the AppUpdatesPresenter under test.
#InjectMock AppUpdatesPresenter appUpdatesPresenter;
Now the test case doesn't rely on canned interaction but real implementation of your code.
#Test
public void test_Stop_Service() throws Exception {
appUpdatesPresenter.stopService();
verify(serviceManager).stopService();
}

How do I use Mockito to mock a protected method?

I’m using Mockito 1.9.5. How do I mock what is coming back from a protected method? I have this protected method …
protected JSONObject myMethod(final String param1, final String param2)
{
…
}
However, when I attempt to do this in JUnit:
final MyService mymock = Mockito.mock(MyService.class, Mockito.CALLS_REAL_METHODS);
final String pararm1 = “param1”;
Mockito.doReturn(myData).when(mymock).myMethod(param1, param2);
On the last line, I get a compilation error “The method ‘myMethod’ is not visible.” How do I use Mockito to mock protected methods? I’m open to upgrading my version if that’s the answer.
This is not an issue with Mockito, but with plain old java. From where you are calling the method, you don't have visibility. That is why it is a compile-time issue instead of a run-time issue.
A couple options:
declare your test in the same package as the mocked class
change the visibilty of the method if you can
create a local (inner) class that extends the mocked class, then mock this local class. Since the class would be local, you would have visibility to the method.
Responding to the request for a code sample of option 3 from John B's answer:
public class MyClass {
protected String protectedMethod() {
return "Can't touch this";
}
public String publicMethod() {
return protectedMethod();
}
}
#RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
class MyClassMock extends MyClass {
#Override
public String protectedMethod() {
return "You can see me now!";
}
}
#Mock
MyClassMock myClass = mock(MyClassMock.class);
#Test
public void myClassPublicMethodTest() {
when(myClass.publicMethod()).thenCallRealMethod();
when(myClass.protectedMethod()).thenReturn("jk!");
}
}
You can use Spring's ReflectionTestUtils to use your class as it is and without needing of change it just for tests or wrap it in another class.
public class MyService {
protected JSONObject myProtectedMethod(final String param1, final String param2) {
return new JSONObject();
}
public JSONObject myPublicMethod(final String param1) {
return new JSONObject();
}
}
And then in Test
#RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {
#Mock
private MyService myService;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(myService.myPublicMethod(anyString())).thenReturn(mock(JSONObject.class));
when(ReflectionTestUtils.invokeMethod(myService, "myProtectedMethod", anyString(), anyString())).thenReturn(mock(JSONObject.class));
}
}
Something like following worked for me, using doReturn() and Junit5's ReflectionSupport.
[Note: I tested on Mockito 3.12.4]
ReflectionSupport.invokeMethod(
mymock.getClass()
// .getSuperclass() // Uncomment this, if the protected method defined in the parent class.
.getDeclaredMethod("myMethod", String.class, String.class),
doReturn(myData).when(mymock),
param1,
param2);
John B is right, this is because the method you're trying to test is protected, it's not a problem with Mockito.
Another option on top of the ones he has listed would be to use reflection to gain access to the method. This will allow you to avoid changing the method you are testing, and avoid changing the pattern you use to write tests, and where you store these tests. I've had to do this myself for some tests where I was not allowed to change the existing code base which included a large number of private methods that needed to be unit tested.
These links explain Reflection and how to use it very well, so I will link to them rather than copy:
What is reflection and whit is it useful
How to test a class that has private methods, fields, or inner classes
WhiteBox.invokeMethod() can be handy.
public class Test extend TargetClass{
#Override
protected Object method(...) {
return [ValueYouWant];
}
}
In Spring, you can set it high high-priority like this:
#TestConfiguration
public class Config {
#Profile({"..."})
#Bean("...")
#Primary // <------ high-priority
public TargetClass TargetClass(){
return new TargetClass() {
#Override
protected WPayResponse validate(...) {
return null;
}
};
}
}
It is the same to override the origin bean.

doNothing void method with params in other class

I currently have 2 classes:
public class aClass{
public void meth1(){
bClass b = new bClass();
b.meth2();// i dont want to call this method
//buss logic
}
}
public class bClass{
public void meth2(){
// some logic
}
}
Currently I am creating a junit test case for the meth1 in the aClass.
However, I don't want to call the meth2 in the bClass, just execute the busslogic in aClass.
The Classes aClass and bClass are fixed - I cannot(and won't) change the code on aClass and bClass.
I tried many things like #injectmock and doNothing using mokito and power mock, but meth2 is always gets called when I am calling meth1 in aClass.
What can I do to fix this?
You can do this with Powermockito. The link explains all the details. Below is how it might look like for your example.
#RunWith(PowerMockRunner.class)
#PrepareForTest(aClass.class)
public class aClassTesting {
#Mock
bClass mockB;
#Test
public void testMeth1(){
//prepare mocks
whenNew(bClass.class).withNoArguments().thenReturn(mockB);
doNothing().when(mockB).meth2();
//run it
aClass instance = new aClass();
aClass.meth1();
//asserts and verify
verifyNew(bClass.class).withNoArguments();
verify(mockB, times(1)).meth2();
}
}
EDIT:
When you mock out the bClass instance using #Mock then it replaces all the methods in that instance with mock methods. If you only want to mock some of the methods in bCLas, then you must spy the bCLass instance instead of mocking it. Spy only mocks the methods you want. So just replace #Mock with #Spy in my example then only meth2 would be blocked but not any other methods in bClass.
As it stands, with the constraint that you cannot change AClass, then the only solution is to use Powermock (or similar tools) as Jose Martinez mentioned above. On its own, Mockito can't replace constructors (which are treated like static method calls), so your only solution is to rewrite aClass.
For any future readers that find this question who don't have that constraint, a quick refactor of bClass will make this much easier to test.
public class aClass{
public void meth1(){
meth1(new bClass());
}
/** Package private for testing. */
public void meth1(bClass b){
b.meth2();
//buss logic
}
}
At this point, you can pass a mock bClass and call the single-parameter overload to test your business logic. You can also move b.meth2() out of the single-parameter method and into the public parameterless method, if it behaves more like a setup call.
You can use the mockito...
public class aClass {
private final bClass b;
public aClass() {
this.b = new bClass()
}
public aClass(bClass b) {
this.b = b;
}
....
}
#RunWith(MockitoJUnitRunner.class)
public class aClassTest {
#Mock
private final bClass b;
#InjectMocks
private aClass a;
....
}

How to have individual SetUp functions in Junit

I have two test functions and for each I want to have different #Before methods. How to achieve this ?
Although it seems to be convenient to organize all the test under the same class, for your case I think the best option is to separate the tests into different classes, each one with his corresponding setUp.
An alternative (I prefer the previous option) could be call the setUp directly in your test method, like the example as follows:
public class FooTest {
public void setUpMethod1() {
// do setUp things
}
public void setUpMethod2() {
// do setUp things
}
#Test
public void testMethod1() {
setUpMethod1();
// Test
}
#Test
public void testMethod2() {
setUpMethod2();
// Test
}
}
Only as a curiosity (IMO not recomended for your case), you can override the default junit RunListener with your own implementation. Method testStarted is executed before every test and you have access to class and methodName to be able to identify the running test. Dummy sample:
public class MyRunListener extends RunListener {
#Override
public void testStarted(Description description) throws Exception {
//...
Class testClass = description.getClass();
String methodName = description.getMethodName();
//...
}
}
Hope it helps.

Unit testing a Play controller using mocks

The title pretty much says it all. I would like to set up a traditional JUnit test to mock a controller's dependencies and run tests against actions.
I've found that I can achieve my goal like this:
public class AccountsControllerTest {
private controllers.Accounts accountsController;
#Test
public void test() {
running(fakeApplication(), new Runnable() {
public void run() {
accountsController = new controllers.Accounts();
accountsController.setAccountsWorkflow(mock(workflow.Accounts.class));
}
});
}
}
The obvious problem here is that I'm instantiating my class under test and injecting mock dependencies from the test method itself, when I should be doing that in the setup() method. It seems that the setup() method is useless if I'm going to test my controller in a traditional way.
Of course I can test controllers the way Play recommends, but my application is dependent on an external SOAP web service, so I need unit tests to show that our code is working when their servers are down.
So, what's the best way to unit test a Play controller using mocks while still taking advantage of setup() and teardown() methods?
Edit
I realize I'm assuming some knowledge here, so for those who are unaware, controller instantiation in a unit test must be wrapped in a running() function or Play! will throw a runtime exception saying that no application has been started.
You could accomplish this using Mockito and Play's FakeApplication and setting the static Http.Context variable.
This way you can write the test like all other JUnit test.
Example:
...
import static play.test.Helpers.status;
import play.test.FakeApplication;
import play.test.Helpers;
import play.mvc.Http;
import play.mvc.Result;
...
#RunWith(MockitoJUnitRunner.class)
public class ApplicationTest {
public static FakeApplication app;
#Mock
private Http.Request request;
#BeforeClass
public static void startApp() {
app = Helpers.fakeApplication();
Helpers.start(app);
}
#Before
public void setUp() throws Exception {
Map<String, String> flashData = Collections.emptyMap();
Http.Context context = new Http.Context(request, flashData, flashData);
Http.Context.current.set(context);
}
#Test
public void testIndex() {
final Result result = Application.index();
assertEquals(play.mvc.Http.Status.OK, status(result));
}
#AfterClass
public static void stopApp() {
Helpers.stop(app);
}