title: Cannot run Robolectric test in Android Studio using Realm + PowerMockito
body: <p>I was using Realm, PowerMockito and Robolectric to write test for my app. The test is running fine if I use <code>./gradlew test</code>, but if I run with the configuration in Android Studio. It will show error as below.</p>
  
  <p><code>com.thoughtworks.xstream.converters.ConversionException: Cannot construct org.powermock.modules.junit4.rule.PowerMockStatement$1 as it does not have a no-args constructor : Cannot construct org.powermock.modules.junit4.rule.PowerMockStatement$1 as it does not have a no-args constructor</code></p>
  
  <p>Does anyone have a solution for this problem? Here is the test that I wrote in Kotlin.</p>
  
  <pre><code>@RunWith(RobolectricGradleTestRunner::class)
  @Config(application = TestApplication::class, constants = BuildConfig::class, sdk = intArrayOf(21))
  @PowerMockIgnore("org.mockito.*", "org.robolectric.*", "android.*")
  @SuppressStaticInitializationFor("io.realm.internal.Util")
  @PrepareForTest(RealmCore::class, RealmLog::class, Realm::class, RealmResults::class, RealmQuery::class)
  class RealmMiddlewareTest {
  
      data class TestState(var item: List&lt;Item&gt; = listOf())
  
      lateinit var realmMock: Realm
  
      lateinit var mockRealmResults: RealmResults&lt;Item&gt;
  
      val mockResults = arrayListOf(
              Item().apply {
                  title = "item#1"
              },
              Item().apply {
                  title = "item#2"
              },
              Item().apply {
                  title = "item#3"
              }
      )
  
      @get:Rule
      val rule = PowerMockRule()
  
      @Before
      fun setUp() {
          PowerMockito.mockStatic(RealmCore::class.java)
          PowerMockito.mockStatic(RealmLog::class.java)
          PowerMockito.mockStatic(Realm::class.java)
          PowerMockito.mockStatic(RealmResults::class.java)
          Realm.init(RuntimeEnvironment.application)
          // Create the mock
          realmMock = PowerMockito.mock(Realm::class.java)
  
          // TODO: Better solution would be just mock the RealmConfiguration.Builder class. But it seems there is some
          // problems for powermock to mock it (static inner class). We just mock the RealmCore.loadLibrary(Context) which
          // will be called by RealmConfiguration.Builder's constructor.
          PowerMockito.doNothing().`when`(RealmCore::class.java)
          RealmCore.loadLibrary(Matchers.any(Context::class.java))
          `when`(Realm.getDefaultInstance()).thenReturn(realmMock)
  
          mockRealmResults = PowerMockito.mock(RealmResults::class.java) as RealmResults&lt;Item&gt;
  
          val mockQuery = PowerMockito.mock(RealmQuery::class.java)
          `when`(realmMock.where(Item::class.java)).thenReturn(mockQuery as RealmQuery&lt;Item&gt;)
          `when`(mockQuery.findFirst()).thenReturn(mockResults[0])
          `when`(mockQuery.findAll()).thenReturn(mockRealmResults)
  
          `when`(mockRealmResults.iterator()).thenReturn(mockResults.iterator())
          `when`(mockRealmResults.size).thenReturn(mockResults.size)
  
  
      }
  
      @Test
      fun realm_transaction_action_successfully_committed() {
          val testReducer = ReducerFn&lt;TestState&gt; { state, action -&gt;
              if (action is RealmTransactionAction.Committed) {
                  if (action.payloadType == Item::class.java.canonicalName) {
                      assertThat(action.payload as Item, sameInstance(mockResults[0]))
                  }
              }
              state
          }
          val store = SimpleStore(TestState(), testReducer).applyMiddleware(RealmMiddleware())
  
          val transaction: (Realm) -&gt; Item = {
              it.copyToRealm(mockRealmResults[0])
          }
  
          val action = RealmTransactionAction.create(transaction = transaction)
          store.dispatch(action)
  
          verify(realmMock, times(1)).executeTransaction(Mockito.any())
          verify(realmMock, times(1)).close()
      }
  
      @RealmClass
      open class Item() : RealmObject() {
          open var title: String = ""
      }
  }
  </code></pre>