Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 如何使用Mockito模拟我的超类的方法_Java_Unit Testing_Mockito - Fatal编程技术网

Java 如何使用Mockito模拟我的超类的方法

Java 如何使用Mockito模拟我的超类的方法,java,unit-testing,mockito,Java,Unit Testing,Mockito,我在模拟没有引用的方法的数据时遇到了一些问题,例如getMethod();不知道mocking框架如何知道它。下面是我面临的问题,我无法在我的代码中设置HttpRequest和URIInfo的代码 是否有可能绕过该方法 Class A { private HttpServletRequest httpRequest; private UriInfo uriInfo; public HttpServletRequest getReq() { return httpRequest; }

我在模拟没有引用的方法的数据时遇到了一些问题,例如getMethod();不知道mocking框架如何知道它。下面是我面临的问题,我无法在我的代码中设置HttpRequest和URIInfo的代码

是否有可能绕过该方法

Class A {

private HttpServletRequest httpRequest;
private UriInfo uriInfo;

public HttpServletRequest getReq() {
    return httpRequest;
}
public void setReq(HttpServletRequest req) {
    this.httpRequest = req;
}


public UriInfo getUriInfo() {
    return uriInfo;
}
public void setUriInfo(UriInfo uriInfo) {
    this.uriInfo = uriInfo;
}}



class B extends A {

// some code

}

class C extends B {

protected Object executeCall(Object beIn) throws Exception{

prepareUpdateConfigurationRequest();
// some other methods.
return "";
}
private void prepareUpdateConfigurationRequest() {
implPutCustomerProductOrderIdProductConfigurationsImpl.setReq(getReq());
implPutCustomerProductOrderIdProductConfigurationsImpl.setUriInfo(getUriInfo());
}}
//使用Mockito框架测试类

@RunWith(MockitoJUnitRunner.class)
public class CTest {

@Mock
private A a = Mockito.mock(A.class);
@InjectMocks
private C c = new C();

private ImplBackEndInput implBackEndInput;

@Test
public void testExecuteCallObject() {



    implBackEndInput = new ImplBackEndInput();

    UriInfo uriInfo = Mockito.mock(UriInfo.class);

    Mockito.when(a.getUriInfo()).thenReturn(uriInfo);
    Mockito.when(a.getReq()).thenReturn(httpServletRequest);

    try {
        c.executeCall(implBackEndInput);
    } catch (Exception e) {
    }

}
}

受保护或私有方法不能使用Mockito进行模拟,我建议如果您使用spring在测试包中创建DummyC类,在springConfig中引用它作为父类,并使其在调用时仅返回对象。这样,该类将使用该方法作为不需要测试的真实类的旁路

不能使用Mockito模拟Protected或privates方法,我建议如果您使用spring在测试包中创建DummyC类,在springConfig中将其作为父类引用,并使其在调用时仅返回对象。这样,该类将使用该方法作为不需要测试的真实类的旁路

考虑到对非静态方法的一些静态引用-
A.getUriInfo()
,以及各种其他错误,我不太确定您的代码是如何编译的。对于使用同一对象的getter调用setter也没有什么意义:

implPutCustomerProductOrderIdProductConfigurationsImpl.setReq(getReq());
implPutCustomerProductOrderIdProductConfigurationsImpl.setUriInfo(getUriInfo());
但是,为了回答可能会出现在这里的其他人的问题,您根本不需要在测试类中模仿类型A(此处):

你真的不需要这两条线。事实上,您可以完全删除
A
的mock(这一行):
@mock private A=Mockito.mock(A.class)

相反,只要做:

c.setUriInfo(uriInfo);
c.setReq(httpServletRequest);

这是因为C扩展了A,这意味着A的所有方法在未被重写时都由C继承。因此,如果在C实例上调用未被重写的setter方法,它将直接转到A的方法。在调用上面显示的set方法之后,当调用
C.getUriInfo()
时,它将把您作为参数传入的对象返回给
setUriInfo(uriInfo)方法。这里根本不需要模拟。

考虑到对非静态方法的一些静态引用-
A.getUriInfo()
,以及各种其他错误,我不太确定您的代码是如何编译的。对于使用同一对象的getter调用setter也没有什么意义:

implPutCustomerProductOrderIdProductConfigurationsImpl.setReq(getReq());
implPutCustomerProductOrderIdProductConfigurationsImpl.setUriInfo(getUriInfo());
但是,为了回答可能会出现在这里的其他人的问题,您根本不需要在测试类中模仿类型A(此处):

你真的不需要这两条线。事实上,您可以完全删除
A
的mock(这一行):
@mock private A=Mockito.mock(A.class)

相反,只要做:

c.setUriInfo(uriInfo);
c.setReq(httpServletRequest);

这是因为C扩展了A,这意味着A的所有方法在未被重写时都由C继承。因此,如果在C实例上调用未被重写的setter方法,它将直接转到A的方法。在调用上面显示的set方法之后,当调用
C.getUriInfo()
时,它将把您作为参数传入的对象返回给
setUriInfo(uriInfo)方法。这里根本不需要模拟。

如何从测试类调用
executeCall
,其可见性受
保护
?@InjectMocks private C=new C();您可以尝试添加下面这行代码吗?如何从测试类调用
executeCall
,其可见性受
保护
?@InjectMocks private C=new C();你能试着添加下面这行代码吗?对大写字母A的混淆表示歉意。。在代码中已更正。除了剩余的语法错误之外,我的答案仍然正确。您不需要模拟调用
a.getUriInfo()
,只需调用
c.setUriInfo(uriInfo)
,即可返回正确的对象。这是由于java继承造成的。对于大写字母A的混淆,我深表歉意。。在代码中已更正。除了剩余的语法错误之外,我的答案仍然正确。您不需要模拟调用
a.getUriInfo()
,只需调用
c.setUriInfo(uriInfo)
,即可返回正确的对象。这是由于java继承。