PowerMock(mit mockito) einfaches unit-test-Fehler
Ich bin mit Mockito + PowerMock zu schreiben, ein einfacher unit test für die folgende singleton-Klasse:
public class MyService {
private static MyService service;
private List<School> schoolList;
private MyService(){
//test case error complains here!
School school = new School();
schoolList.add(school);
}
public static Singleton getInstance( ) {
return service;
}
protected static void printSchool( ) {
School school = schoolList.get(0);
print(school);
}
}
Meinem Test-Fall:
@RunWith(PowerMockRunner.class)
public class MyServiceTest {
@PrepareForTest({MyService.class})
@Test
public void testPrintSchool() {
//enable mock static function
PowerMockito.mockStatic(MyService.class);
MyService mockService = PowerMockito.mock(MyService.class);
PowerMockito.when(MyService.getInstance())
.thenReturn(mockService);
}
}
Ich mit meinen test, erhielt aber die folgende Fehlermeldung:
java.lang.RuntimeException: Invoking the beforeTestMethod method on PowerMock test listener org.powermock.api.extension.listener.AnnotationEnabler@3ab19451 failed.
at com.xyz.MyService.<init>(MyService.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.mockito.internal.util.reflection.FieldInitializer$ParameterizedConstructorInstantiator.instantiate(FieldInitializer.java:257)
at org.mockito.internal.util.reflection.FieldInitializer.acquireFieldInstance(FieldInitializer.java:124)
at org.mockito.internal.util.reflection.FieldInitializer.initialize(FieldInitializer.java:86)
at org.mockito.internal.configuration.injection.ConstructorInjection.processInjection(ConstructorInjection.java:52)
...
Wie Sie sehen können, die Fehlermeldung beschwert sich über MyService.java:12
Linie 12, das ist die Linie School school = new School();
im MyService
Konstruktor.
Warum bekomme ich diese Fehler, wie man es loswerden?
Du musst angemeldet sein, um einen Kommentar abzugeben.
@PrepareForTest({MyService.class}) ist ein class-level annotation.
Sollten Sie fügen Sie es an der gleichen Stelle wie @RunWith(PowerMockRunner.class)
Finden Sie mehr Informationen zur github