在我無法重構的源類中(所以我不能使用建議here)有= new XXX的對象創建。我必須嘲笑他們的函數調用X()。call()。PowerMockito whenNew返回null
爲此,我使用powermock的whenNew()函數。但是我在這個例子中在測試LoginSuccessHandler的類中有null。在這裏我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 = {[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]}
這裏是結果,當它擊中,吸氣:
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]}
返回null和對象創建返回空了。什麼導致這個問題?
你應該改變你的生產代碼,以便它得到的實例'OrcaAuthorizationServiceBeanService'注入。然後你可以用普通的Mockito來嘲笑它。 –
正如我所說的旅店第一句話,我不能改變生產代碼,並使用普通的mockito。這就是爲什麼我使用powermockito – cmlonder
有你用'MockitoAnnotations.initMocks(this)初始化模仿;' – pvpkiran