import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({GlobalUtil.class})
public class Test_StaticMethodCall {
public static boolean called = false;
@Test
public void test_check_static_error_method_is_called() throws Exception {
//arrange
called = false;
// mock static GlobalUtil
PowerMockito.mockStatic(GlobalUtil.class);
//set called on true if error method is called
PowerMockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Test_StaticMethodCall.called = true;
return null;
}
}).when(GlobalUtil.class, "error", Mockito.anyObject(), Mockito.any(Throwable.class));
//act
new ClassToBeTested().testMethod();
assertEquals(true, called);
}
}
class GlobalUtil {
public static void error(Object msg, Throwable t) {
//do something
}
}
class ClassToBeTested {
public void testMethod() {
Boolean nullBollean = null;
try {
if (nullBollean) {
//do something
}
} catch (Exception ex) {
GlobalUtil.error("Somthing is wrong", ex);
}
}
}
Niciun comentariu:
Trimiteți un comentariu