简体   繁体   English

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

[英]how to mock methods of my superclass using Mockito

I am facing issues in mocking the data for the methods which are being called without the reference eg getMethod(); 我在模拟没有引用的方法的数据时遇到了问题,例如getMethod();。 don't know how will mocking framework know about it. 不知道模拟框架如何知道它。 Below is the code for which am facing issue am not able to set HttpRequest and URIInfo in my code. 下面是面临问题的代码,无法在我的代码中设置HttpRequest和URIInfo。

Is it possible to bypass the method. 是否可以绕过该方法。

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

// Test class using Mockito Framework //使用Mockito Framework测试类

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

}
}

Protected or privates methods cannot be mocked using Mockito, I would suggest if you are using spring to you create a DummyC class in your test package, reference that as a parent in the springConfig and make it just return the object when is called. 使用Mockito无法模拟受保护的方法或私有方法,我建议如果您使用spring在测试包中创建一个DummyC类,请在springConfig中将其作为父类引用,并使其在调用时返回对象。 In that way the class will use that method as a by-pass to the real class that you don't need to test. 这样,该类将使用该方法作为不需要测试的真实类的旁路。

I'm not really sure how your code is compiling, given some static references to non-static methods - A.getUriInfo() - and various other errors. 考虑到对非静态方法的一些静态引用A.getUriInfo() -以及其他各种错误,我不太确定您的代码如何编译。 It also doesn't make a whole lot of sense that you're calling setters using getters for the same object: 您使用同一个对象的getter来调用setter也没有什么意义:

implPutCustomerProductOrderIdProductConfigurationsImpl.setReq(getReq());
implPutCustomerProductOrderIdProductConfigurationsImpl.setUriInfo(getUriInfo());

But to answer your question for anyone else who might wind up here, you don't need to be mocking type A in the test class at all (here): 但是要回答可能会出现在这里的其他任何人的问题,您根本不需要在测试类中嘲笑类型A(在这里):

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

You really don't need those two lines at all. 您真的根本不需要这两行。 In fact, you can remove the mock of A entirely (this line): @Mock private A a = Mockito.mock(A.class); 实际上,您可以完全删除A的模拟内容(此行): @Mock private A a = Mockito.mock(A.class);

Instead, just do: 相反,只需执行以下操作:

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

This works because C extends A, meaning all of A's methods, when not overridden, are inherited by C. So if you call the non-overridden setter methods on the instance of C, it will go straight to the methods of A. After calling the set methods shown above, when you call c.getUriInfo() , it will return the object you passed in as a parameter to the setUriInfo(uriInfo); 之所以有效,是因为C扩展了A,这意味着A的所有方法在未被覆盖时都由C继承。因此,如果在C实例上调用非覆盖的setter方法,它将直接转到A的方法。上面显示的set方法,当您调用c.getUriInfo() ,它将把您作为参数传递的对象返回给setUriInfo(uriInfo); method. 方法。 No need to mock at all here. 这里完全不需要嘲笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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