我试图在Kotlin中使用jMockit模拟一个静态方法:
object: MockUp<System>() {
@Mock
fun getProperty(name: String) = "tagB"
}
但是我收到以下错误:
无法加载Logmanager&#34; tagB&#34; 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) 在java.util.logging.LogManager $ 1.run(LogManager.java:195) 在java.util.logging.LogManager $ 1.run(LogManager.java:181) at java.security.AccessController.doPrivileged(Native Method) 在java.util.logging.LogManager。(LogManager.java:181) 在java.util.logging.Logger.getPlatformLogger(Logger.java:572) 在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) 在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) 在java.util.Formatter.getZero(Formatter.java:2283) 在java.util.Formatter。(Formatter.java:1892) 在java.util.Formatter。(Formatter.java:1914) 在java.lang.String.format(String.java:2940) 在org.junit.runner.Description.formatDisplayName(Description.java:114) 在org.junit.runner.Description.createTestDescription(Description.java:73) 在io.kotlintest.TestCase.getDescription(testcase.kt:45) 在io.kotlintest.TestBase.descriptionForSuite $ kotlintest_main(TestBase.kt:153) 在io.kotlintest.TestBase.getDescription $ kotlintest_main(TestBase.kt:39) 在io.kotlintest.KTestJUnitRunner.getDescription(KTestJUnitRunner.kt:11) 在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) 在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 在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) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 记录配置类&#34; tagB&#34;失败 java.lang.ClassNotFoundException:tagB ...
其他带有期望障碍的方法也没有成功。
如何在Kotlin中模拟静态方法?
答案 0 :(得分:4)
你应该像这样模拟系统:
class MockSystem : MockUp<System>() {
@Mock
fun getProperty(name: String) = "tagB"
}
class MockTest {
val m = MockSystem();
@Test fun test() {
Assert.assertEquals(System.getProperty("hello"), "tagB")
}
}