Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PowerMockito whenNew返回null_Java_Junit_Mockito_Powermockito - Fatal编程技术网

Java PowerMockito whenNew返回null

Java PowerMockito whenNew返回null,java,junit,mockito,powermockito,Java,Junit,Mockito,Powermockito,在我无法重构的源类中(因此我不能使用advices),存在=new XXX的对象创建。我必须模拟它们的函数调用X().call() 为此,我使用powermock的whenNew()函数。但是我正在测试的类中有null,在本例中是loginsucesshandler。这里是我的LoginSuccessHandlerTest类: @RunWith(PowerMockRunner.class) 公共类登录成功HandlerTest{ @InjectMock私有LoginSAccessHandler

在我无法重构的源类中(因此我不能使用advices),存在=new XXX的对象创建。我必须模拟它们的函数调用X().call()

为此,我使用powermock的whenNew()函数。但是我正在测试的类中有null,在本例中是loginsucesshandler。这里是我的LoginSuccessHandlerTest类:

@RunWith(PowerMockRunner.class)
公共类登录成功HandlerTest{
@InjectMock私有LoginSAccessHandler LoginSAccessHandler;
@模拟私密的GuiSessionDAO GuiSessionDAO;
@模拟私有UserAuthorityDAO UserAuthorityDAO;
@模拟私有OrcaAuthorizationServiceBean或OrcaAuthorizationServiceBean;
@模拟私有OrcaAuthorizationServiceBeanService OrcaAuthorizationServiceBeanService;
@模拟私有GetUserRoleSeturnModel UserRoleSeturnModel;
私有认证;
私有MockHttpServletRequest请求;
私有MockHttpServletResponse;
@以前
公共作废设置(){
请求=新的MockHttpServletRequest();
response=新的MockHttpServletResponse();
身份验证=新的TestingAuthenticationToken(“foo”、“foo”、“foo”);
}
@PrepareForTest({loginsAccessHandler.class})
@试验
AuthenticationSuccess()上的公共void引发异常{
whenNew(OrcaAuthorizationServiceBeanService.class.),带参数(URL.class.),然后返回(OrcaAuthorizationServiceBeanService);
p(“模仿逆戟鲸的叫声”);
当(orcaAuthorizationServiceBeanService.getOrcaAuthorizationServiceBeanPort())。然后返回(orcaAuthorizationServiceBean);
当(orcaAuthorizationServiceBean.getUserRoles(any(Header.class),anyString()).thenReturn(userRoleSeturnModel);
when(userRolesReturnModel.getUserRoles()).thenReturn(Collections.singletonList(“ADMIN”));
p(“启动模拟登录”);
onAuthenticationSuccess(请求、响应、身份验证);
assertEquals(MockHttpServletResponse.SC_OK,response.getStatus());
}
私有void p(字符串s){
系统输出打印项次;
}
这里我得到空值

OrcaAuthorizationServiceBeanService service = new OrcaAuthorizationServiceBeanService(new URL(url));
调试时,我可以确认powermockito正在运行以模拟此对象创建,并且正在调用此方法:

public static synchronized NewInvocationControl<?> putNewInstanceControl(Class<?> type, NewInvocationControl<?> control) {
        return newSubstitutions.put(type, control);
    }
下面是当它击中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} 
公共静态同步NewInvocationControl getNewInstanceControl(类类型){
返回newSubstitutions.get(类型);
}
类型={Class@277}“类java.net.URL”
新替代={HashMap@1823}尺寸=1
0={HashMap$Node@2195}“class com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService”->
键={Class@1755}“类com.ttech.timsgui.ldap.OrcaAuthorizationServiceBeanService”
值={MockitoNewInvocationControl@2137} 
这将返回null,对象创建也将返回null。是什么导致了此问题?

请尝试

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

您应该更改生产代码,使其注入
或aauthorizationservicebeanservice
的实例。然后您可以使用普通Mockito对其进行模拟。正如我在第一句中所说,我无法更改生产代码并使用普通Mockito。这就是为什么我使用powermockito使用
MockitoAnnotations.ini初始化mocktMocks(this);
我同意pvpkiran-查看您显示的代码,您忽略了PowerMockRunner可能不会初始化Mockito@Mock注释的mocks的事实。我现在添加并重试了,仍然返回null。看起来它找不到具有新URL(URL)的构造函数,而新URL正在调用whenNew(或aaAuthorizationServiceBeanService.class)。withArguments(URL.class)不会提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论。-可以使用“withAnyArguments”而不是“withArguments”来解决此问题
whenNew(OrcaAuthorizationServiceBeanService.class).withAnyArguments().thenReturn(orcaAuthorizationServiceBeanService);