简体   繁体   中英

PowerMockito whenNew returns null

In the source class that which I can't refactor (so i can't use advices here ) there are object creations with = new XXX. And i have to mock their function calls X().call().

For this purpose i am using powermock's whenNew() function. But i am having null in the class that i am testing which is LoginSuccessHandler in this case. Here my LoginSuccessHandlerTest class:

@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);
    }

And here i get null

OrcaAuthorizationServiceBeanService service = new OrcaAuthorizationServiceBeanService(new URL(url));

When i debug, i can confirm that powermockito is running to mock this object creation and this method is being called:

public static synchronized NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control) {
        return newSubstitutions.put(type, control);
    }

And those are the parameters:

type = {Class@1755} "class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService"
 cachedConstructor = null
 newInstanceCallerCache = null
 name = "com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService"
 classLoader = {MockClassLoader@2118} 
 reflectionData = {SoftReference@2119} 
 classRedefinedCount = 0
 genericInfo = null
 enumConstants = null
 enumConstantDirectory = null
 annotationData = null
 annotationType = null
 classValueMap = null
control = {MockitoNewInvocationControl@2093} 
 substitute = {InvocationSubstitute$$EnhancerByMockitoWithCGLIB$$4d9f6379@2109} "invocationSubstitute"
  CGLIB$BOUND = true
  CGLIB$CALLBACK_0 = {PowerMockMethodInterceptorFilter@2115} 
  CGLIB$CALLBACK_1 = {SerializableNoOp@2116} 

And here is the result when it hits the getter:

public static synchronized NewInvocationControl<?> getNewInstanceControl(Class<?> type) {
    return newSubstitutions.get(type);
}

type = {Class@277} "class java.net.URL"
newSubstitutions = {HashMap@1823}  size = 1
 0 = {HashMap$Node@2195} "class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService" -> 
  key = {Class@1755} "class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService"
  value = {MockitoNewInvocationControl@2137} 

this returns null and object creation returns null too. What causes this problem?

尝试,

whenNew(OrcaAuthorizationServiceBeanService.class).withAnyArguments().thenReturn(orcaAuthorizationServiceBeanService);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM