在我无法重构的源类中(因此我无法使用建议here),有对象创建= new XXX。我必须模拟他们的函数调用X()。call()。
为此我正在使用powermock的whenNew()函数。但是我在类中测试的是null,在这种情况下是LoginSuccessHandler。这是我的LoginSuccessHandlerTest类:
@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);
}
这里我得到了空白
OrcaAuthorizationServiceBeanService service = new OrcaAuthorizationServiceBeanService(new URL(url));
当我调试时,我可以确认powermockito正在运行以模拟此对象创建并且正在调用此方法:
public static synchronized NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control) {
return newSubstitutions.put(type, control);
}
这些是参数:
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}
这是击中吸气器时的结果:
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}
返回null,对象创建也返回null。是什么导致了这个问题?
答案 0 :(得分:0)
尝试,
whenNew(OrcaAuthorizationServiceBeanService.class).withAnyArguments().thenReturn(orcaAuthorizationServiceBeanService);