简体   繁体   English

在 Java JUnit 中模拟 Class.class

[英]Mock Class.class in Java JUnit 5

I am trying to test a overridden method that takes in a parameter of type Class<? extends HttpMessageConverter<?>>我正在尝试测试一个接受Class<? extends HttpMessageConverter<?>>类型参数的重写方法Class<? extends HttpMessageConverter<?>> as an input. Class<? extends HttpMessageConverter<?>>作为输入。 As this parameter is not being used in my implementation, I want to mock it (or in worst case I can go ahead with actual initialization of the instance).由于此参数未在我的实现中使用,我想模拟它(或者在最坏的情况下,我可以在实际初始化实例之前先进行 go)。

@Mock
Class<? extends HttpMessageConverter<?>> selectedConverterType;  

This is how I am mocking it (although I have tried using Mockito.mock() , but even that did not work)这就是我 mocking 它的方式(尽管我尝试过使用Mockito.mock() ,但即使这样也没有用)

@Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
            ServerHttpResponse response) {.....}

This is the method I am trying to test.这是我正在尝试测试的方法。

Some observation:一些观察:
It fails when MockCreationValidator try to validate the type of mock in MockSettingsImpl .MockCreationValidator尝试验证MockSettingsImpl中的模拟类型时,它会失败。 This happens only for Class type, other objects are being mocked as expected.这只发生在Class类型中,其他对象正在按预期模拟。

As the concerned instance is not being used in implementation, passing it as null did the job for me.由于相关实例未在实现中使用,因此将其传递为null为我完成了这项工作。 It is not a good idea to do so, but I am running out of time and I want to reach code coverage limit.这样做不是一个好主意,但我的时间不多了,我想达到代码覆盖率限制。

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

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