如何在 Kotlin 中模拟静态方法?

How to mock static methods in Kotlin?

我正在尝试在 Kotlin 中使用 jMockit 模拟一个静态方法:

object: MockUp<System>() {
  @Mock
  fun getProperty(name: String) = "tagB"
}

但是我得到以下错误:

Could not load Logmanager "tagB" java.lang.ClassNotFoundException: tagB at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.util.logging.LogManager.run(LogManager.java:195) at java.util.logging.LogManager.run(LogManager.java:181) at java.security.AccessController.doPrivileged(Native Method) at java.util.logging.LogManager.(LogManager.java:181) at java.util.logging.Logger.getPlatformLogger(Logger.java:572) at java.util.logging.LoggingProxyImpl.getLogger(LoggingProxyImpl.java:41) at sun.util.logging.LoggingSupport.getLogger(LoggingSupport.java:100) at sun.util.logging.PlatformLogger$JavaLoggerProxy.(PlatformLogger.java:602) at sun.util.logging.PlatformLogger$JavaLoggerProxy.(PlatformLogger.java:597) at sun.util.logging.PlatformLogger.(PlatformLogger.java:239) at sun.util.logging.PlatformLogger.getLogger(PlatformLogger.java:198) at sun.util.locale.provider.LocaleServiceProviderPool.config(LocaleServiceProviderPool.java:142) at sun.util.locale.provider.LocaleProviderAdapter.(LocaleProviderAdapter.java:165) at java.text.DecimalFormatSymbols.getInstance(DecimalFormatSymbols.java:178) at java.util.Formatter.getZero(Formatter.java:2283) at java.util.Formatter.(Formatter.java:1892) at java.util.Formatter.(Formatter.java:1914) at java.lang.String.format(String.java:2940) at org.junit.runner.Description.formatDisplayName(Description.java:114) at org.junit.runner.Description.createTestDescription(Description.java:73) at io.kotlintest.TestCase.getDescription(testcase.kt:45) at io.kotlintest.TestBase.descriptionForSuite$kotlintest_main(TestBase.kt:153) at io.kotlintest.TestBase.getDescription$kotlintest_main(TestBase.kt:39) at io.kotlintest.KTestJUnitRunner.getDescription(KTestJUnitRunner.kt:11) at com.intellij.junit4.JUnit4IdeaTestRunner.getDescription(JUnit4IdeaTestRunner.java:123) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:99) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Logging configuration class "tagB" failed java.lang.ClassNotFoundException: tagB ...

其他带有期望块的方法也没有成功。

如何在 Kotlin 中模拟静态方法?

你应该像这样模拟系统:

class MockSystem : MockUp<System>() {
    @Mock
    fun getProperty(name: String) = "tagB"
}


class MockTest {

    val m = MockSystem();

    @Test fun test() {
        Assert.assertEquals(System.getProperty("hello"), "tagB")
    }
}