Kotlin ConversionException:无法将类型…trustAllCerts$1转换为类型javax.net.ssl.X509TrustManager

Kotlin ConversionException:无法将类型…trustAllCerts$1转换为类型javax.net.ssl.X509TrustManager,kotlin,realm,powermock,x509trustmanager,Kotlin,Realm,Powermock,X509trustmanager,我正在尝试使用PowerMock模拟一个领域实例并配置一个presenter实例。很久以前,我一直在尝试模拟王国,但它太难了,特别是在科特林,所以我在过去放弃了。 我能够解决一些问题,但现在我面临一个新的问题,我无法轻松解决任何问题,因此我需要了解Powermock的人的帮助 很抱歉代码行太多 该项目使用Kotlin编写,库版本: testImplementation 'org.mockito:mockito-core:2.23.0' testImplementation 'com.nh

我正在尝试使用PowerMock模拟一个领域实例并配置一个presenter实例。很久以前,我一直在尝试模拟王国,但它太难了,特别是在科特林,所以我在过去放弃了。 我能够解决一些问题,但现在我面临一个新的问题,我无法轻松解决任何问题,因此我需要了解Powermock的人的帮助

很抱歉代码行太多

该项目使用Kotlin编写,库版本:

  testImplementation 'org.mockito:mockito-core:2.23.0'
  testImplementation 'com.nhaarman:mockito-kotlin:1.5.0'
  testImplementation 'org.robolectric:robolectric:4.3'
  testImplementation 'io.reactivex.rxjava2:rxjava:2.2.10'
  testImplementation "org.powermock:powermock-module-junit4-rule:2.0.2"
  testImplementation "org.powermock:powermock-module-junit4:2.0.2"
  testImplementation "org.powermock:powermock-api-mockito2:2.0.2"
  testImplementation "org.powermock:powermock-classloading-xstream:2.0.2"
以下是测试实现:

@RunWith(value = RobolectricTestRunner::class)
@PowerMockRunnerDelegate(value = RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1])
@PowerMockIgnore(value = ["org.robolectric.*", "org.mockito.*", "android.*"])
@SuppressStaticInitializationFor("io.realm.internal.Util")
@PrepareForTest(value = [Realm::class, RealmConfiguration::class, RealmQuery::class, RealmResults::class, RealmCore::class, RealmLog::class])
class SomePresenterTest {

  var mockRealm: Realm? = null
  private lateinit var presenter: Presenter

  @Rule
  @JvmField var rule = PowerMockRule()

  private val okHttpClientManager: OkHttpClientManager by lazy {
    OkHttpClientManager(...)
  }

  @Before
  fun setUp() {
    setupRealm()
    presenter = PresenterImpl(okHttpClientManager)
  }

  private fun setupRealm() {

    mockStatic(RealmCore::class.java)
    mockStatic(RealmLog::class.java)
    mockStatic(Realm::class.java)
    mockStatic(RealmConfiguration::class.java)
    RealmCore.loadLibrary(RuntimeEnvironment.application)
    Realm.init(RuntimeEnvironment.application)

    val mockRealm = mock(Realm::class.java)
    val mockRealmConfig = mock(RealmConfiguration::class.java)

//    doNothing().`when`(RealmCore::class.java)
    whenNew(RealmConfiguration::class.java).withAnyArguments().thenReturn(mockRealmConfig)

    `when`(Realm.getDefaultInstance()).thenReturn(mockRealm)

    this.mockRealm = mockRealm

  }

}
调用
OkHttpClientManager()
时会出现问题

以下是我如何配置它:

//...Generic OkHttp configuration before

val trustAllCerts: Array<X509TrustManager> = arrayOf(object: X509TrustManager {
    @SuppressLint("TrustAllX509TrustManager")
    override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
    @SuppressLint("TrustAllX509TrustManager")
    override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
    override fun getAcceptedIssuers(): Array<X509Certificate> { return emptyArray() }
})
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, trustAllCerts, java.security.SecureRandom())
builder.sslSocketFactory(sslContext.socketFactory, trustAllCerts[0])
builder.hostnameVerifier(HostnameVerifier { _, _ -> true })
你知道怎么解决这个问题吗? 非常感谢。 佩德罗

com.thoughtworks.xstream.converters.ConversionException: Cannot convert type com.app.my.OkHttpClientManager$trustAllCerts$1 to type javax.net.ssl.X509TrustManager
---- Debugging information ----
message             : Cannot convert type com.app.my.OkHttpClientManager$trustAllCerts$1 to type javax.net.ssl.X509TrustManager
class               : sun.security.ssl.AbstractTrustManagerWrapper
required-type       : sun.security.ssl.AbstractTrustManagerWrapper
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/target/okHttpClientManager$delegate/value/okHttpClient/sslSocketFactoryOrNull/context/trustManager/tm
line number         : 260
class[1]            : sun.security.ssl.SSLContextImpl$TLSContext
class[2]            : sun.security.ssl.SSLSocketFactoryImpl
class[3]            : okhttp3.OkHttpClient
class[4]            : com.app.my.OkHttpClientManager
class[5]            : kotlin.InitializedLazyImpl
class[6]            : com.app.my.rules.presenter.HomePresenterTest
class[7]            : org.junit.internal.runners.statements.InvokeMethod
class[8]            : org.junit.internal.runners.statements.RunBefores
class[9]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[10]           : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : not available
-------------------------------

    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:456)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1486)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1466)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1337)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1328)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:64)
    at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:53)
    at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:33)
    at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:40)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)