How do you bypass static method calls? - junit

I have this line which is interferring in a unit test:
OtherClass.staticMethodThatWillErrorIfCalled().isAvailable();
If it wasn't static I could just mock OtherClass and then do this:
Mockito.doReturn(null).when(mockedOtherClass).staticMethodThatWillErrorIfCalled();
Mockito.doReturn(true).when(mockedOtherClass).isGuiMode();
and the fact that it will error if called makes my attempts at using powermockito futile.
I'm not sure how I can do this. All I want to do is skip over this line (it's an if check) and continue on as if it had returned true. What is the best way to do this?

I would require more info to give a more specific answer but this is what I am thinking...
First tell PowerMockito that you will be mocking a static method in OtherClass.
#RunWith(PowerMockRunner.class)
#PrepareForTest(OtherClass.class)
These are class level annotations that go on your unit testing class.
Then mock what to do when that method is called.
PowerMockito.mockStatic(OtherClass.class);
Mockito.when(OtherClass.isAvailable()).thenReturn(Boolean.TRUE);
Do this in your #Before method on your unit testing.

Related

I am getting missing behaviour definition for the preceding method call:

I am new to unit test. I am trying to create test case of a method which is calling an static method. For this static method I am getting "getting missing behaviour definition for the preceding method call:"
EasyMock can't mock static methods. So that's probably the problem. But having actual failing code will help providing a full answer.

Junit test case with Mockito

I am creating junit test cases for my project. I have the below code, where I would like to create a mock,
String propertyFilePath = System.getProperty("path.to.properties");
Resource propertyFile = new FileSystemResourceLoader().getResource(propertyFilePath);
Properties properties = PropertiesLoaderUtils.loadProperties(propertyFile);
I am using junit and mockito-core jar. I tried with below code,
System.setProperty("path.to.properties", "dummyPathToProperties"); //invalid Path
Properties properties = mock(Properties.class);
Resource propertyFile = new FileSystemResourceLoader().getResource("dummyPathToProperties");
when(PropertiesLoaderUtils.loadProperties(propertyFile)).thenReturn(properties);
With above code it throws error when mocking loadProperties method. How can I mock a spring static class and return my mock properties object ?
Any help will be really appreciated.
Mocking static methods requires you to go down the full nine yards and make use of PowerMock. The exact steps to mock static methods are outlined in their documentation for example.
In essence:
Use the #RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
Use the #PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case.
Use PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class.
Use PowerMock.replay(ClassThatContainsStaticMethod.class) to change the class to replay mode.
Use PowerMock.verify(ClassThatContainsStaticMethod.class) to change the class to verify mode.
But of course: consider not using PowerMock; by changing your code so that you don't have to mock the static call. But of course, it is kinda weird to add a wrapper around such a framework-provided static method.

How to unmock using powermock

I am need to unmock a static method & call the real method in the class constructor as this will give connection to the DB. Now using powermock when I say #RunWith(PowerMockRunner.class) it doesnt allow me to call the real method. This was possible in mockito but I need to use powermock as I need to mock other static methods too.
public TestESMock() throws ConfigurationException{
DatabaseImpl dbImpl=DatabaseImpl.newDatabaseImpl(null);
}
Can someone tell me how to I do this.
There was no problem with powermockito. I was getting exception as powermockito 1.5.6 was not able to connect with SQL Server JDBC4

Mocking #UriInfo using EasyMock or PowerMock

I have a REST service class in which uriInfo object is automatically injected through #UriInfo annotation. Now, while writing JUnit for this class, I want to get a mock object created for this UriInfo object without introducing any new setter methods into the tested class just for the sake of setting the mocked UriInfo into it. Kindly let me know if you have any suggestions. We are using EasyMock and PowerMock.
You can use Powermock's Whitebox to modify the internal state of an object. One of the simplest invocations is:
Whitebox.setInternalState(tested, myMock);

How to test a void method with JMock

How to test a void method i.e. method that doesn't return anything in JMock?
To test a method that doesn't return anything, regardless of the testing or mocking framework you're using, you test the effect of a call to the method.
With JMock that likely means that you create a mock of something the code you're testing should call, set things up so that your mock is used instead of a real object, and set and verify expectations for calls to that mock.
I might be able to get more specific if you can add specifics to your question.
void methods generally make some changes in the value of the fields of the class. If the field of the class is not private then you can access it in your test class after calling the void method in your test method to assert if you are getting the expected value.