5

I'm trying to mock a static method with jMockit in Kotlin:

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

But I get the following error:

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$1.run(LogManager.java:195) at java.util.logging.LogManager$1.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 ...

Other approaches with expectations blocks weren't successful, too.

How can I mock a static method in Kotlin?

deamon
  • 89,107
  • 111
  • 320
  • 448
  • Why don't you refactor your code so that it allows to pass whatever value you're accessing through `System.getProperty` through an explicit API? – yole Jul 13 '16 at 10:21
  • In this case, there is nothing to mock: just call `System.setProperty(...)`. – Rogério Jul 13 '16 at 16:42
  • @Rogério `System.setProperty` is not an option (acutally it is the current "solution"), because it interferes with other tests. – deamon Jul 14 '16 at 12:48

1 Answers1

4

You should mock System like this:

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


class MockTest {

    val m = MockSystem();

    @Test fun test() {
        Assert.assertEquals(System.getProperty("hello"), "tagB")
    }
}
Aleosha
  • 151
  • 1
  • 6