In der Quellklasse, die ich nicht umgestalten kann (so kann ich nicht Advices here verwenden) gibt es Objektkreationen mit = new XXX. Und ich muss ihre Funktionsaufrufe vortäuschen X(). Call().PowerMockito whenNew gibt null zurück
Zu diesem Zweck benutze ich PowerMerchs whenNew() -Funktion. Aber ich habe null in der Klasse, die ich teste, die in diesem Fall LoginSuccessHandler ist. Hier meine LoginSuccessHandlerTest Klasse:
@RunWith(PowerMockRunner.class)
public class LoginSuccessHandlerTest {
@InjectMocks private LoginSuccessHandler loginSuccessHandler;
@Mock private GuiSessionDAO guiSessionDAO;
@Mock private UserAuthorityDAO userAuthorityDAO;
@Mock private OrcaAuthorizationServiceBean orcaAuthorizationServiceBean;
@Mock private OrcaAuthorizationServiceBeanService orcaAuthorizationServiceBeanService;
@Mock private GetUserRolesReturnModel userRolesReturnModel;
private Authentication authentication;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
authentication = new TestingAuthenticationToken("foo", "foo", "foo");
}
@PrepareForTest({LoginSuccessHandler.class})
@Test
public void onAuthenticationSuccess() throws Exception {
whenNew(OrcaAuthorizationServiceBeanService.class).withArguments(URL.class).thenReturn(orcaAuthorizationServiceBeanService);
p("Mocking Orca WS calls");
when(orcaAuthorizationServiceBeanService.getOrcaAuthorizationServiceBeanPort()).thenReturn(orcaAuthorizationServiceBean);
when(orcaAuthorizationServiceBean.getUserRoles(any(Header.class), anyString())).thenReturn(userRolesReturnModel);
when(userRolesReturnModel.getUserRoles()).thenReturn(Collections.singletonList("ADMIN"));
p("Starting mock log in");
loginSuccessHandler.onAuthenticationSuccess(request, response, authentication);
assertEquals(MockHttpServletResponse.SC_OK, response.getStatus());
}
private void p(String s) {
System.out.println(s);
}
Und hier i null erhalten
OrcaAuthorizationServiceBeanService service = new OrcaAuthorizationServiceBeanService(new URL(url));
Wenn ich debuggen ich, dass powermockito bestätigen kann, läuft diese Objekterstellung zu verspotten und diese Methode aufgerufen wird:
public static synchronized NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control) {
return newSubstitutions.put(type, control);
}
Und das sind die Parameter:
type = {[email protected]} "class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService"
cachedConstructor = null
newInstanceCallerCache = null
name = "com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService"
classLoader = {[email protected]}
reflectionData = {[email protected]}
classRedefinedCount = 0
genericInfo = null
enumConstants = null
enumConstantDirectory = null
annotationData = null
annotationType = null
classValueMap = null
control = {[email protected]}
substitute = {[email protected]} "invocationSubstitute"
CGLIB$BOUND = true
CGLIB$CALLBACK_0 = {[email protected]}
CGLIB$CALLBACK_1 = {[email protected]}
Und hier ist das Ergebnis, wenn es den Getter hits:
public static synchronized NewInvocationControl<?> getNewInstanceControl(Class<?> type) {
return newSubstitutions.get(type);
}
type = {[email protected]} "class java.net.URL"
newSubstitutions = {[email protected]} size = 1
0 = {[email protected]} "class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService" ->
key = {[email protected]} "class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService"
value = {[email protected]}
diese null zurück und Objekterstellung kehrt zu null. Was verursacht dieses Problem?
Sie sollten Sie Produktionscode ändern, damit es die Instanz von 'OrcaAuthorizationServiceBeanService' wird injiziert. Dann könntest du es mit dem einfachen Mockito verhöhnen. –
Wie ich schon im ersten Satz gesagt habe, kann ich den Produktionscode nicht ändern und einfach nur Mockito verwenden. Deshalb verwende ich powermockito – cmlonder
haben Sie initiierte Mocks mit 'MockitoAnnotations.initMocks (this);' – pvpkiran