When I'm trying to mock a static method with a String input, mocked stub is getting executed when I give a specific String, but when I use anyString(), it does not work as expected.
public class Foo {
public static String staticInput(String s) {
System.out.println("staticInput called");
return "static " + s;
}
}
#RunWith(PowerMockRunner.class)
#PrepareForTest({Foo.class})
public class TestMockito {
#Test
public void test1() throws Exception {
PowerMockito.spy(Foo.class);
PowerMockito.doReturn("dummyStaticStub").when(Foo.class, "staticInput", "1");
System.out.println(Foo.staticInput("1"));
}
#Test
public void test2() throws Exception {
PowerMockito.spy(Foo.class);
PowerMockito.doReturn("dummyStaticIn").when(Foo.class, "staticInput", anyString());
System.out.println(Foo.staticInput("1"));
}
}
test1 prints:
dummyStaticStub
test2 prints:
staticInput called
static 1
You can change the approach a little and use PowerMockito.mockStatic instead
#RunWith(PowerMockRunner.class)
#PrepareForTest({Foo.class})
public class TestMockito {
#Test
public void test1() throws Exception {
PowerMockito.mockStatic(Foo.class);
Mockito.when(Foo.staticInput("1")).thenReturn("dummyStaticStub");
System.out.println(Foo.staticInput("1"));
}
#Test
public void test2() throws Exception {
PowerMockito.mockStatic(Foo.class);
PowerMockito.when(Foo.staticInput(anyString())).thenReturn("dummyStaticIn");
System.out.println(Foo.staticInput("1"));
}
}
Reference Using PowerMock with Mockito: Mocking Static Method
Related
Junit test case getting failed:
existing static mocking registration must be deregistered
Test class:
#RunWith(MockitoJUnitRunner.class)
public class DeleteReportServiceTest {
#InjectMocks
private ReturnCheckController returnCheckController;
#Mock
MockedStatic<DigitalGatewayRESTClient> mockDigiGateway;
#Before
public void setUp() throws Exception {
this.returnCheckController = new ReturnCheckController();
this.mockDigiGateway = Mockito.mockStatic(DigitalGatewayRESTClient.class);
}
#Test
public void testdeleteReport() throws Exception {
String input = "{\"clientId\": \"1\", \"applicationId\": \"9010\"}";
String response = "{\"success\":true,\"successText\":\"Manual adjustment record deleted\"}";
String expected = "{\"status\":200}";
JSONObject returnJson = new JSONObject(response);
mockDigiGateway.when((Verification) DigitalGatewayRESTClient.getDGRESTConnection(Mockito.any())).thenReturn(returnJson);
String actual = returnCheckController.deleteReport(input);
Assert.assertNotNull(actual);
Assert.assertEquals(expected, actual);
}
#After
public void after() {
mockDigiGateway.close();
}
}
Closing the static mocking still am getting the error.
You can NOT use #Mock and MockedStatic at same time.
Instead, if you want to stub static method of DigitalGatewayRESTClient, you should create a MockedStatic<DigitalGatewayRESTClient> additionally, like this:
private MockedStatic<DigitalGatewayRESTClient> mockedStaticDigiGateway;
Then initialize mockedStaticDigiGateway:
#Before
public void setUp() throws Exception {
this.mockedStaticDigiGateway = Mockito.mockStatic(DigitalGatewayRESTClient.class);
// other setup...
}
and modify your test case, stub mockedStaticDigiGateway.when... instead:
#Test
public void testdeleteReport() throws Exception {
// arrange ...
mockedStaticDigiGateway.when((Verification) DigitalGatewayRESTClient.getDGRESTConnection(Mockito.any()))
.thenReturn(returnJson);
// assert...
}
Finally, close mockedStaticDigiGateway after test is finished:
#After
public void after() {
mockedStaticDigiGateway.close();
}
I think it will work correctly although one year passed.
I'm trying to write a test class that requires the use of a certain setup. When there's only 1 setup, this is easy with #BeforeEach:
#BeforeEach public void setup() {
// my setup code
}
#Test public void test1() {
// ...
}
#Test public void test2() {
// ...
}
#Test public void test3() {
// ...
}
But what can I do when there are several setups to choose from? Of course, I could forget the #BeforeEach altogether and ask colleagues to call the setup method they'd like to use:
#Test public void test1() {
setupA();
// ...
}
#Test public void test2() {
setupB();
// ...
}
#Test public void test3() {
setupB();
// ...
}
But this no longer forces the use of one of my setup methods. Is there a way to implement a "parametrized #BeforeEach"? Something like (made-up syntax):
enum SetupType {A, B, C};
#BeforeEach public void setup(SetupType setupType) {
switch (setupType) {
case A:
setupA();
break;
case B:
setupB();
break;
case C:
setupC();
break;
default:
fail("Unrecognized setup.");
}
#Test
#BeforeEachParameter(SetupType.A)
public void test1() {
// ...
}
#Test
#BeforeEachParameter(SetupType.B)
public void test2() {
// ...
}
#Test
#BeforeEachParameter(SetupType.B)
public void test3() {
// ...
}
Or even better, baking it into the #Test annotation?
#TestWithSetupA public void test1() {
// ...
}
#TestWithSetupB public void test2() {
public void test2() {
// ...
}
#TestWithSetupB public void test3() {
public void test3() {
// ...
}
yes and it's indeed pretty simple. You can use the test annotation Tags and in the before each setup inject the TestInfo object but you will need JUnit 5.
Here's a working JUnit
public class TestBeforeEach {
#BeforeEach
public void setUp(TestInfo testInfo) {
System.out.println(testInfo.getTags());
}
#Test
#Tag( "setup1" )
public void test1() {
}
#Test
#Tag( "setup2" )
public void test2() {
}
Hope this helps. Cheers!
Assuming I have the following classes
public class StaticClass {
public static void staticMethod() throws SomeException {
System.out.println("staticMethod");
}
private StaticClass() {
}
}
and
public class SomeClass {
public void someMethod() {
try {
StaticClass.staticMethod();
}catch(SomeException ex) {
System.out.println("SomeException occurred");
return;
}
System.out.println("SomeException didn't occur");
}
}
which I'm testing with
#RunWith(PowerMockRunner.class)
#PrepareForTest(StaticClass.class)
public class SomeClassTest {
#Test
public void testStaticMethod() throws Exception {
mockStatic(StaticClass.class);
doThrow(new SomeException("unimportant message")).when(StaticClass.class,
"staticMethod");
//test something where exception is needed
SomeClass instance = new SomeClass();
try {
instance.someMethod();
fail("IllegalStateException expected");
}catch(IllegalStateException expected) {
}
//now test something where exception isn't needed
instance.someMethod();
}
}
How can I undo the static mocking/the configuration to throw SomeException so that I can test the code after the try-catch block in the second instance.someMethod()?
PowerMock: How to unmock a method? doesn't apply because there's no mock reference to pass to Mockito.reset and passing StaticClass causes java.lang.ClassCastException: java.lang.Class cannot be cast to org.mockito.internal.creation.bytebuddy.MockAccess.
SomeException simply extends Exception.
A SSCCE is provided at https://gitlab.com/krichter/powermock-undo-statik-mocking.
I'm using PowerMock 1.7.3.
My opinion, but in general a unit test should exercise a single code path. (I think of this as applying single responsibility to a test method.)
Bu my suggestion about splitting the tests does solve the problem. I don't know the details, but #PrepareForTest provides a fresh StaticClass for each test.
These separate tests work:
#Test
public void testStaticMethodWhenSomethingUnexpectedHappens() throws Exception {
mockStatic(StaticClass.class);
// changed exception type
doThrow(new IllegalStateException("unimportant message")).when(StaticClass.class, "staticMethod");
SomeClass instance = new SomeClass();
try {
instance.someMethod();
fail("IllegalStateException expected");
} catch (IllegalStateException expected) {
}
// added verification
verifyStaticMethodWasInvokedOneTime();
}
#Test
public void testStaticMethodHappyPath() throws Exception {
mockStatic(StaticClass.class);
doNothing().when(StaticClass.class, "staticMethod");
SomeClass instance = new SomeClass();
instance.someMethod();
// added verification
verifyStaticMethodWasInvokedOneTime();
}
private void verifyStaticMethodWasInvokedOneTime() throws SomeException {
verifyStatic(StaticClass.class);
StaticClass.staticMethod();
}
For anyone wondering how to reset PowerMocks e.x. for those pesky private static final loggers...
There was an issue (see Karl's comment in accepted solution) addressing it and the solution was to use #PrepareForTest in the method level.
so in your test method you need the annotation
/*
* Test for MyClass.java which uses a private static final logger
*/
public class MyStaticMockTest {
static final Logger logger = PowerMockito.mock(Logger.class);
#BeforeClass
public static void setup() {
PowerMockito.mockStatic(LoggerFactory.class);
PowerMockito.when(LoggerFactory.getLogger(MyClass.class))
.thenReturn(MyStaticMockTest.logger);
}
#Test
#PrepareForTest({LoggerFactory.class, Logger.class})
public void testit() {
MyClass mc = new MyClass();
mc.methodWithSomeLogging();
Mockito.verify(MyStaticMockTest.logger).info("some message");
}
#Test
#PrepareForTest({LoggerFactory.class, Logger.class})
public void testit() {
MyClass mc = new MyClass();
mc.anotherMethodWithSomeLoggingButUsingSameMessage();
//Method will pass and not complain about info being called 2x
Mockito.verify(MyStaticMockTest.logger, Mockito.times(1)).info("some message");
}
}
if you want every single method reset, just put the #PrepareForTest decorator on the class instead of the method
Jmockit is very powerful, but sometimes I cannot understand what it does behind the scene, so I have a question regarding jmockit. Hopefully the more experienced programmers on here could help shine some light on this situation :)
I have the following two classes in two separate files:
public class SmallClass {
String a;
SmallClass(String arg) throws Exception {
a = arg;
}
public String getString() {
return a;
}
}
And
public class BigClass {
private static final SmallClass smallClass;
static {
try {
smallClass = new SmallClass("dummy");
} catch (Exception e) {
throw new IllegalStateException("Could not initialized", e);
}
}
public static String getString() {
return smallClass.getString();
}
}
Now, I have a class to test BigClass:
public class BigClassTest {
#Test
public void testGet() throws Exception {
///CLOVER:OFF
new MockUp<SmallClass>() {
#Mock
public void $init(String string) throws Exception {
//Do nothing
}
#Mock
public String getString() {
return "dummyString";
}
};
///CLOVER:ON
Assert.assertEquals("dummyString", BigClass.getString());
}
#Test(expected = ExceptionInInitializerError.class)
public void testException() throws Exception {
///CLOVER:OFF
new MockUp<SmallClass>() {
#Mock
public void $init(String string) throws Exception{
throw new Exception("Mocked Exception");
}
};
///CLOVER:ON
BigClass.getString();
}
}
If I run each of these independently, then they each passes. But if I run the whole test file, then the first test fails with:
java.lang.NoClassDefFoundError: Could not initialize class BigClass
I also tried tearing down the mock after each test like this, but it doesn't help:
public class BigClassTest {
MockUp<SmallClass> smallClassMockUp;
#Test
public void testGet() throws Exception {
///CLOVER:OFF
smallClassMockUp = new MockUp<SmallClass>() {
#Mock
public void $init(String string) throws Exception {
//Do nothing
}
#Mock
public String getString() {
return "dummyString";
}
};
///CLOVER:ON
Assert.assertEquals("dummyString", BigClass.getString());
smallClassMockUp.tearDown();
}
#Test(expected = ExceptionInInitializerError.class)
public void testException() throws Exception {
///CLOVER:OFF
smallClassMockUp = new MockUp<SmallClass>() {
#Mock
public void $init(String string) throws Exception{
throw new Exception("Mocked Exception");
}
};
///CLOVER:ON
BigClass.getString();
smallClassMockUp.tearDown();
}
}
Any help would be appreciated. Thank you in advance!
The occurrence of NoClassDefFoundError, in a case like this, is not because the class wasn't found by the JVM (it was), but because its static initialization has failed (by throwing an exception or error from the execution of a static initializer). Once this happens, the class is left in an invalid/uninitialized state and cannot be used in the same JVM instance anymore.
For reference, see the "Initialization of classes and interfaces" section in the JLS.
Also, note that the order in which tests execute is not necessarily the textual order they appear in the test class. Here, testException (the second test) runs first. So, when testGet runs, the class is invalid and the JVM throws the error.
class StaticClass {
public static String a(){ return "a"; }
public static String ab(){ return a()+"b"; }
}
I want to mock StaticClass::a so that it returns "x" and the call to StaticClass.ab() results in "xb"...
I find it very hard in PowerMock and TestNG...
the exact code I am testing righ now:
class StaticClass {
public static String A() {
System.out.println("Called A");
throw new IllegalStateException("SHOULD BE MOCKED AWAY!");
}
public static String B() {
System.out.println("Called B");
return A() + "B";
}
}
#PrepareForTest({StaticClass.class})
public class StaticClassTest extends PowerMockTestCase {
#Test
public void testAB() throws Exception {
PowerMockito.spy(StaticClass.class);
BDDMockito.given(StaticClass.A()).willReturn("A");
assertEquals("AB", StaticClass.B()); // IllegalStateEx is still thrown :-/
}
}
I have Maven dependencies on:
<artifactId>powermock-module-testng</artifactId>
and
<artifactId>powermock-api-mockito</artifactId>
Why not try something like :
PowerMockito.mockStatic(StaticClass.class);
Mockito.when(StaticClass.a()).thenReturn("x");
Mockito.when(StaticClass.ab()).thenCallRealMethod();
I think this can be accomplished with a Partial Mock.
PowerMock.mockStaticPartial(Mocked.class, "methodToBeMocked");
This might be of help: http://avricot.com/blog/index.php?post/2011/01/25/powermock-%3A-mocking-a-private-static-method-on-a-class