I'm not able to capture the parameter of a static method. The static method is called in the tested method and then called a second time in the verifications block but this time the parameter is null so I have a NullPointerException...
Here's my code :
@Tested
private Service testService;
@Test
public void test {
// Tested method
Deencapsulation.invoke(testService, "testMethod");
// Verifications
new Verifications() {
NotificationsUtils utils;
{
HashMap<String, Object> map;
NotificationsUtils.generateXML(anyString, map = withCapture());
// NullPointerException is thrown here
// because in generateXML method map is null and map.entrySet() is used.
}
};
}
How can I capture the map variable when generateXML is called ?
Thanks
Apparently, your test never declared NotificationsUtils
to be mocked.
The following complete example works, though:
static class Service {
void testMethod() {
Map<String, Object> aMap = new HashMap<String, Object>();
aMap.put("someKey", "someValue");
NotificationsUtils.generateXML("test", aMap);
}
}
static class NotificationsUtils {
static void generateXML(String s, Map<String, Object> map) {}
}
@Tested Service testService;
@Test
public void test(@Mocked NotificationsUtils utils) {
Deencapsulation.invoke(testService, "testMethod");
new Verifications() {{
Map<String, Object> map;
NotificationsUtils.generateXML(anyString, map = withCapture());
assertEquals("someValue", map.get("someKey"));
}};
}