title: How to mock objects in Kotlin?
body: <p>I want to test a class that calls an object (static method call in java) but I'm not able to mock this object to avoid real method to be executed.</p>
  
  <pre><code>object Foo {
      fun bar() {
          //Calls third party sdk here
      }
  }
  </code></pre>
  
  <p>I've tried different options like <a href="http://mockk.io/#object-mocks" rel="noreferrer">Mockk</a>, <a href="https://stackoverflow.com/questions/37977320/how-to-mock-a-kotlin-singleton-object">How to mock a Kotlin singleton object?</a> and using PowerMock in the same way as in java but with no success.</p>
  
  <p>Code using PowerMockito:</p>
  
  <pre><code>@RunWith(PowerMockRunner::class)
  @PrepareForTest(IntentGenerator::class)
  class EditProfilePresenterTest {
  
      @Test
      fun shouldCallIntentGenerator() {
  
          val intent = mock(Intent::class.java)
  
          PowerMockito.mockStatic(IntentGenerator::class.java)
          PowerMockito.`when`(IntentGenerator.newIntent(any())).thenReturn(intent) //newIntent method param is context
  
         presenter.onGoToProfile()
  
         verify(view).startActivity(eq(intent))        
  
      }
  }
  </code></pre>
  
  <p>With this code I get </p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: Parameter specified as non-null is null: method com.sample.test.IntentGenerator$Companion.newIntent, parameter context</p>
  </blockquote>
  
  <p>any() method is from <a href="https://github.com/nhaarman/mockito-kotlin" rel="noreferrer">mockito_kotlin</a>. Then If I pass a mocked context to newIntent method, it seems real method is called.</p>