Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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:未在方法中注入模拟对象_Java_Unit Testing_Mocking_Mockito_Powermock - Fatal编程技术网

Java:未在方法中注入模拟对象

Java:未在方法中注入模拟对象,java,unit-testing,mocking,mockito,powermock,Java,Unit Testing,Mocking,Mockito,Powermock,我要测试的代码行是: private static final EntityType WORK_FLOW_ENTITY_TYPE = //Assigned public WorkflowRequest getWFRequestFromHerdInput(HerdInput herdInput) throws NonRetriableException { ActionRequest request = CoralHerdUtils.getRequestData(herdI

我要测试的代码行是:

private static final EntityType WORK_FLOW_ENTITY_TYPE = //Assigned
public WorkflowRequest getWFRequestFromHerdInput(HerdInput herdInput) throws
        NonRetriableException {
    ActionRequest request = CoralHerdUtils.getRequestData(herdInput);
    Document document = request.getHerdDocument();
    List<Entity> entityList = document.getEntitiesByType(WORK_FLOW_ENTITY_TYPE);
    Entity entity = entityList.get(0);
    WorkflowRequest workflowRequest = null;
    try {
        workflowRequest = (WorkflowRequest) entity.asCommonsObject();
    } catch (DocumentException e) {
        throw new NonRetriableException("Object cannot be converted to WorkflowRequest");
    }
    return workflowRequest;
}
private static final EntityType WORK\u FLOW\u ENTITY\u TYPE=//已分配
公共工作流请求getWFRequestFromHerdInput(HerdInput HerdInput)抛出
不可重试异常{
ActionRequest=CoralHerdUtils.getRequestData(herdInput);
Document Document=request.getHerdDocument();
List entityList=document.getEntityByType(工作\流程\实体\类型);
实体=entityList.get(0);
WorkflowRequest WorkflowRequest=null;
试一试{
workflowRequest=(workflowRequest)实体.asCommonsObject();
}捕获(文档异常){
抛出新的不可重试异常(“对象无法转换为WorkflowRequest”);
}
返回工作流请求;
}
我想测试try-catch的catch部分。我同时使用PowerMockito和Mockito。对于测试,我编写了以下内容:

@Test(expected = NonRetriableException.class)
public void test_GetWFRequestFromHerdInput_fail() throws NonRetriableException, DocumentException {
    PowerMockito.mockStatic(CoralHerdUtils.class);
    PowerMockito.when(CoralHerdUtils.getRequestData(herdInput)).thenReturn(actionRequest);
    EntityType WORKFLOW_ENTITYTYPE = new EntityType(new Aspect("DigitalInfluence"),
            "Application", "1.0");
    DocumentFactory docFactory = new DocumentFactory();
    docFactory.registerCommonsClass(WorkflowRequest.class, WORKFLOW_ENTITYTYPE);
    document = docFactory.createDocument();
    document.addEntity(workflowRequest);
    Mockito.when(actionRequest.getHerdDocument()).thenReturn(document);
    List<Entity> entities = Mockito.mock(List.class);
    Entity entity = Mockito.mock(Entity.class);
    entities.add(entity);
    Document documentMock = Mockito.mock(Document.class);
    Mockito.when(documentMock.getEntitiesByType(WORKFLOW_ENTITYTYPE)).thenReturn(entities);
    Mockito.when(entities.get(0)).thenReturn(entity);
    Mockito.when(entity.asCommonsObject()).thenThrow(DocumentException.class);

    WorkflowRequest workflowRequestReturned = herdDocumentHelper.getWFRequestFromHerdInput(herdInput);
    Assert.assertEquals(EXPECTED_DAG_ID, workflowRequestReturned.getDagId());
}
@Test(预期为NonRetriableException.class)
public void test_GetWFRequestFromHerdInput_fail()引发不可重试的异常,DocumentException{
mockStatic(CoralHerdUtils.class);
PowerMockito.when(CoralHerdUtils.getRequestData(herdInput)).thenReturn(actionRequest);
EntityType工作流\u EntityType=新EntityType(新特性(“数字影响”),
“申请书”、“1.0”);
DocumentFactory docFactory=新的DocumentFactory();
docFactory.RegisterCommonClass(WorkflowRequest.class,工作流实体类型);
document=docFactory.createDocument();
文件附录(工作流请求);
Mockito.when(actionRequest.getHerdDocument())。然后返回(document);
列表实体=Mockito.mock(List.class);
实体=Mockito.mock(Entity.class);
实体。添加(实体);
documentdocumentmock=Mockito.mock(Document.class);
Mockito.when(documentMock.getEntitiesByType(工作流\实体类型))。然后返回(实体);
Mockito.when(entities.get(0)).thenReturn(entity);
Mockito.when(entity.asCommonsObject()).thenthow(DocumentException.class);
WorkflowRequest workflowRequestReturned=herdDocumentHelper.getWFRequestFromHerdInput(herdInput);
Assert.assertEquals(预期为_DAG_ID,workflowRequestReturned.getDagId());
}
问题是测试用例没有选择模拟的实体对象,而是在方法内部创建的
entityList.get(0)

如何在方法中强制注入模拟对象,以便测试catch分支?

您需要模拟:

  • CoralHerdUtils.getRequestData
    返回模拟的
    ActionRequest
  • request.getHerdDocument
    返回模拟的
    文档
  • document.getEntitiesByType
    返回包含模拟的
    实体的列表
您需要模拟:

  • CoralHerdUtils.getRequestData
    返回模拟的
    ActionRequest
  • request.getHerdDocument
    返回模拟的
    文档
  • document.getEntitiesByType
    返回包含模拟的
    实体的列表

    • 真正的答案是:你必须理解什么是嘲弄,以及它是如何运作的

      仅仅在某个地方创建一个模拟对象并不能神奇地将该对象“获取”到测试代码中

      因此,一个独特的非答案是:了解模拟框架是如何工作的,然后将其应用到当前的代码库中(如用户talex的答案所述)。例如,开始阅读


      顺便说一句:到目前为止,生产代码中没有任何东西可以证明使用PowerMock(ito)是合理的。因此,如果可能的话,请继续使用plain Mockito

      真正的答案是:你必须了解什么是嘲弄以及它是如何运作的

      仅仅在某个地方创建一个模拟对象并不能神奇地将该对象“获取”到测试代码中

      因此,一个独特的非答案是:了解模拟框架是如何工作的,然后将其应用到当前的代码库中(如用户talex的答案所述)。例如,开始阅读


      顺便说一句:到目前为止,生产代码中没有任何东西可以证明使用PowerMock(ito)是合理的。因此,如果可能的话,请继续使用plain Mockito

      更新了测试用例。因为我在不理解的情况下模仿(1.5年前)。现在我理解了Java中的UTs。向上投票!更新了测试用例。因为我在不理解的情况下模仿(1.5年前)。现在我理解了Java中的UTs。向上投票!更新了测试用例。它仍然没有在测试用例中注入模拟。更新了测试用例。但它并没有在测试用例中注入模拟。