我正在尝试为 Junit 5 测试用例模拟静态类(org.apache.commons.beanutils.Beanutils)。我发现mockito-inline
依赖有助于模拟静态类。我尝试在项目中使用 ,mockito-inline
由于一些奇怪的原因,它在没有库的情况下给我编译错误mockito-core
。
我mockito-core
正在下面:
org.mockito.exceptions.base.MockitoException:
The used MockMaker PowerMockMaker does not support the creation of static mocks
Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.
at com.xx.xx.xx.AvroCopyPropertiesInvocationTargetExceptionScenario(CreditOfferServiceTest.java:1197)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124) …
Run Code Online (Sandbox Code Playgroud) Mockito 3.6 支持在块下模拟静态方法try-with-resources
,如此处所述。
有人可以让我知道静态方法是否使用 Powermock 进行模拟,@Before
或者@BeforeClass
可以Mockito.mockStatic
用来替换它们而无需完全重写测试类吗?
任何人都可以详细说明PowerMock 和 PowerMockito。我什至没有得到 powermockito 的文档。我猜两者都用于以不同的方式模拟静态和私有方法。有什么相似之处和用法?哪一个更好?