在 Spring 5.0.7.RELEASE 中模拟静态方法

Mocking static method in Spring 5.0.7.RELEASE

我有一个 Spring 5.0.7.RELEASE 应用程序,带有一些 WebLayer 测试 我的应用程序中有这个测试:

@RunWith(SpringRunner.class)
@WebAppConfiguration
public class HongoControllerTest  {


    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;



    @Before
    public void setup() {

        mockStatic(TranslationUtils.class);
}

但是当我 运行 测试时我有这个错误:

org.mockito.exceptions.base.MockitoException: 
The used MockMaker SubclassByteBuddyMockMaker 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.

}

我也尝试过这种方法:

@RunWith(PowerMockRunner.class)
@PrepareForTest( TranslationUtils.class )
@WebAppConfiguration
public class HongoControllerTest  {
...
}

但后来控制台出现错误:

java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath.

    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.registerProxyframework(AbstractTestSuiteChunkerImpl.java:146)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.chunkClass(AbstractTestSuiteChunkerImpl.java:181)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:96)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:89)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:49)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBu

我在 Spring 引导应用程序中遇到了这个问题。

<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>

我确实在 pom.xml 中添加了以下依赖项,正如他们在日志中所说的那样,它起作用了:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-inline</artifactId>
  <version>4.0.0</version>
  <scope>test</scope>
</dependency>