首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Mockito Junit正确调用超类方法

如何从Mockito Junit正确调用超类方法
EN

Stack Overflow用户
提问于 2020-06-26 07:00:37
回答 1查看 308关注 0票数 1

我有个基本处理程序

代码语言:javascript
复制
public class BaseHandler {
    protected Object extractInput(RoutingContext rc) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.put("baseKey", "baseValue");
        return jsonObject
    }
}

和一个孩子处理程序

代码语言:javascript
复制
public class ChildHandler extends BaseHandler {

    @Override
    protected Object extractInput(RoutingContext rc) {
        JsonObject jsonObject = (JsonObject) super.extractInput(rc);
        populateFileObjects(rc).ifPresent(jsonArray -> json.put("fileUploads", jsonArray));
        return jsonObject
    }
}

我使用Mockito编写了Junit代码

代码语言:javascript
复制
@Test
public void extractInputTest() {
    ChildHandler handler = spy(new ChildHandler());
    RoutingContext rc = mock(RoutingContext.class);

    JsonObject jsonObject = new JsonObject();
    jsonObject.put("key", "value");

    JsonArray jsonArray = new JsonArray();
    JsonObject fileObject = new JsonObject();
    fileObject.put("fileName", "name");
    jsonArray.add(fileObject);
    Optional<JsonArray> optional = Optional.of(jsonArray);

    doReturn(jsonObject).when((BaseHandler)handler).extractInput(eq(rc));
    doReturn(optional).when(handler).populateFileObjects(rc);

    Object o = handler.extractInput(rc);
    Map<String, Object> map = ((JsonObject) o).getMap();

    Assert.assertEquals("value", ((JsonObject)o).getString("key"));
    Assert.assertEquals(2, map.size()); //Junit fails here
    Assert.assertEquals(JsonArray.class, map.get("fileUploads").getClass());
    Assert.assertEquals(fileObject, ((JsonArray)map.get("fileUploads")).getJsonObject(0));
} 

我尝试在ChildHandler类的extractInput方法的第一行上放置断点,并注意到它从未被调用。

模拟super.method调用的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-26 07:22:13

有一种方法可以这样做,但它不是很干净,但它比使用Java反射做同样肮脏的事情要好。您应该在子类中的另一个方法中提取对超级方法的调用,如下所示:

代码语言:javascript
复制
public class ChildHandler extends BaseHandler {

    @Override
    protected Object extractInput(RoutingContext rc) {
        JsonObject jsonObject = (JsonObject) superExtractInput(rc);
        populateFileObjects(rc).ifPresent(jsonArray -> json.put("fileUploads", jsonArray));
        return jsonObject
    }

    protected Object superExtractInput(RoutingContext rc){
        super.extractInput(rc);
    }
}

你的测试看起来是:

代码语言:javascript
复制
@Test
public void extractInputTest() {
    ChildHandler handler = spy(new ChildHandler());
    RoutingContext rc = mock(RoutingContext.class);

    JsonObject jsonObject = new JsonObject();
    jsonObject.put("key", "value");

    JsonArray jsonArray = new JsonArray();
    JsonObject fileObject = new JsonObject();
    fileObject.put("fileName", "name");
    jsonArray.add(fileObject);
    Optional<JsonArray> optional = Optional.of(jsonArray);

    doReturn(jsonObject).when(handler).superExtractInput(eq(rc));
    doReturn(optional).when(handler).populateFileObjects(rc);

    Object o = handler.extractInput(rc);
    Map<String, Object> map = ((JsonObject) o).getMap();

    Assert.assertEquals("value", ((JsonObject)o).getString("key"));
    Assert.assertEquals(2, map.size()); //Junit fails here
    Assert.assertEquals(JsonArray.class, map.get("fileUploads").getClass());
    Assert.assertEquals(fileObject, ((JsonArray)map.get("fileUploads")).getJsonObject(0));
} 

为了在测试阶段避免这类问题,最好是重于继承

这些课程应该是:

代码语言:javascript
复制
public class BaseHandler {
    protected Object extractInput(RoutingContext rc) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.put("baseKey", "baseValue");
        return jsonObject
    }
}

public class ChildHandler{

    private final BaseHandler baseHandler;

    public ChildHandler(final BaseHandler baseHandler){
        this.baseHandler = baseHandler;
    }

    @Override
    protected Object extractInput(RoutingContext rc) {
        JsonObject jsonObject = (JsonObject) baseHandler.extractInput(rc);
        populateFileObjects(rc).ifPresent(jsonArray -> json.put("fileUploads", jsonArray));
        return jsonObject
    }
}

这一测试将更加明确:

代码语言:javascript
复制
@InjectMocks
ChildHandler testObj;

@Mock
BaseHandler baseHandlerMock;

@Mock
RoutingContext routingContextMock;

@Test
public void extractInputTest() {
    
    JsonObject jsonObject = new JsonObject();
    jsonObject.put("key", "value");

    JsonArray jsonArray = new JsonArray();
    JsonObject fileObject = new JsonObject();
    fileObject.put("fileName", "name");
    jsonArray.add(fileObject);
    Optional<JsonArray> optional = Optional.of(jsonArray);

    when(baseHandlerMock.extractInput(routingContextMock)).thenReturn(routingContextMock);
    doReturn(optional).when(handler).populateFileObjects(routingContextMock);

    Object o = handler.extractInput(routingContextMock);
    Map<String, Object> map = ((JsonObject) o).getMap();

    Assert.assertEquals("value", ((JsonObject)o).getString("key"));
    Assert.assertEquals(2, map.size()); //Junit fails here
    Assert.assertEquals(JsonArray.class, map.get("fileUploads").getClass());
    Assert.assertEquals(fileObject, ((JsonArray)map.get("fileUploads")).getJsonObject(0));
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62589573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档