如何解决Java中的InvalidUseOfMatchers异常-Mockito

时间:2018-11-07 08:19:00

标签: java unit-testing mockito powermockito

我一直在为ProcessorImpl类进行单元测试。在该类中,我从另一个类MetadataUtils创建了一个静态void调用。在MetadataUtils类中,我有:

public class MetadataUtils {    
    private static Metadata metadata  = new Metadata();
    private static ObjectMapper mapper = new ObjectMapper();
    public static void setMetadataFromHeader(String header) throws JsonParseException, JsonMappingException, IOException {
        metadata = mapper.readValue(header, Metadata.class);        
    }
...
}

在我的ProcessorImpl类中,我叫

MetadataUtils.setMetadataFromHeader(getHeader(CsbConstants.METADATA).toString());

在ProcessorImplTest(单元测试)类中,我写道:

PowerMockito.when(processor, method(ProcessorImpl.class, "getHeader"))
        .withArguments(CsbConstants.METADATA)
        .thenReturn(Mockito.any(String.class));
MetadataUtils metadataUtils = Mockito.mock(MetadataUtils.class);
Mockito.doNothing().when(metadataUtils).setMetadataFromHeader(Mockito.any(String.class));

但是我收到了InvalidUseOfMachers异常。您能帮忙解决吗?

1 个答案:

答案 0 :(得分:0)

Mockito.when(mock.doSomething()).thenReturn(...)应该带有实际值,不能返回Mockito.any(...)