Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock static methods in Kotlin?

Tags:

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?

like image 328
deamon Avatar asked Jul 13 '16 08:07

deamon


People also ask

Can we mock static methods?

Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock static methods.

Can static classes be mocked?

The powerful capabilities of the feature-rich JustMock framework allow you to mock static classes and calls to static members like methods and properties, set expectations and verify results. This feature is a part of the fastest, most flexible and complete mocking tool for crafting unit tests.

Can we create static methods in Kotlin?

Android Dependency Injection using Dagger with Kotlin In Java, once a method is declared as "static", it can be used in different classes without creating an object. With static methods, we don't have to create the same boilerplate code for each and every class.

How do you mock a static method in MockK?

Mocking static methodsRather than passing a reference to the class, you pass the class name as a string. You can also choose to pass in a reference to the class, and MockK will figure out the class name. Like object mocks, static mocks behave like spies. The real method will be called if the method is not stubbed.


1 Answers

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")
    }
}
like image 55
Aleosha Avatar answered Sep 28 '22 03:09

Aleosha