when()在testNG模拟

时间:2018-04-10 08:45:31

标签: java spring mocking testng

我在ReferenceConceptServiceImpl课程中有这个methoid用于测试,我在这里使用了Mocking。

@Resource
  private ReferenceConceptDTOMapper referenceConceptDtoMapper;

@Resource
  private TerminologyDefinitionDTOMapper definitionDTOMapper;

    public ReferenceConceptDTO getConceptByTerminologyAndId(final TerminologyDefinitionDTO terminology,
                                                              final String conceptId)
      {
        TerminologyDefinition terminologyDefinition = definitionDTOMapper.load(terminology);
        final ReferenceConcept findRefernaceConceptsByTermAndId = getDAO()
            .findReferenceConceptByTermAndId(terminologyDefinition, conceptId);

        return referenceConceptDtoMapper.convert(findRefernaceConceptsByTermAndId);
      }

我班级的构造函数是:

public ReferenceConceptServiceImpl(final se.cambio.concept.terminology.dto.spi.ReferenceConceptEntityDTOMapper<D> mapper,
                                     final se.cambio.concept.terminology.dto.spi.ReferenceConceptMutableEntityDTOMapper<MD> mutableMapper,
                                     final se.cambio.concept.terminology.persistence.ReferenceConceptDAO dao)
  {
    super(mapper, mutableMapper, dao);
  }

我添加了包含可测试类bean及其依赖项的xml文件。 我还在测试类中添加了必需的依赖项,并尝试测试上面的方法。它就像:

@Test
  public void testGetConceptByTerminologyAndId()
  {
    String conceptId = "test_Concept";
    when(definitionDTOMapper.load(getTerminologyDefinitionDTO())).thenReturn(getTerminologyDefinition());
    when(referenceConceptServiceImpl.getDAO()).thenReturn(mockReferenceConceptDAO);
    when(mockReferenceConceptDAO.findReferenceConceptByTermAndId(getTerminologyDefinition(), conceptId))
        .thenReturn(getReferenceConcept());
    when(referenceConceptDtoMapper.convert(getReferenceConcept())).thenReturn(getReferenceConceptDTO());
    ReferenceConceptDTO actualOutput = referenceConceptServiceImpl .getConceptByTerminologyAndId(getTerminologyDefinitionDTO(), conceptId);
    assertEquals(actualOutput, getReferenceConceptDTO());
  }

这里所有的getter方法都有数据。我的问题是,    在调用getDAO()方法之后,它返回DAO类型的对象。所以我试着通过获取getDAO()的输出来调用findReferenceConceptByTermAndId

问题就在那条线上。

:当(referenceConceptServiceImpl.getDAO())thenReturn(mockReferenceConceptDAO);

错误是:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.

有人可以为我的问题澄清解决方案吗?

1 个答案:

答案 0 :(得分:0)

我自己找到了一个解决方案,我在这里发布;

我在这里做的是添加以下代码:

when(referenceConceptServiceImpl.getDAO()findReferenceConceptByTermAndId(getTerminologyDefinition(), conceptId)).thenReturn(getReferenceConcept());

注意以下两行:

 when(referenceConceptServiceImpl.getDAO()).thenReturn(mockReferenceConceptDAO);
    when(mockReferenceConceptDAO.findReferenceConceptByTermAndId(getTerminologyDefinition(), conceptId))
        .thenReturn(getReferenceConcept());