【问题标题】:Mocking static method in Spring 5.0.7.RELEASE在 Spring 5.0.7.RELEASE 中模拟静态方法
【发布时间】:2021-05-01 12:30:08
【问题描述】:

我有一个 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

【问题讨论】:

    标签: java spring spring-boot junit mocking


    【解决方案1】:

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

    <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>
    

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 2015-08-27
      • 2020-06-21
      • 1970-01-01
      相关资源
      最近更新 更多