我正在为我的SampleActivity编写测试用例。我想从MainActivity开始对其进行测试。
共享SampleActivity及其Test类。
SampleActivity:
class SampleActivity : BaseActivity() {
@Inject
lateinit var router: RoutingContracts.Router
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidInjection.inject(this)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
<----------------Rest of the code------------>
}
共享“我的测试”课程:
@Config(constants = BuildConfig::class, sdk = [24], application = TestApplication::class)
@RunWith(RobolectricTestRunner::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "android.*"
,"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"
, "com.sun.org.apache.xalan.*")
@PrepareForTest(MyFeatureLibrary::class)
class SampleActivityTest {
@get:Rule
public val rule = PowerMockRule()
@Test
fun shouldStartMainActivity() {
PowerMockito.spy<MyFeatureLibrary>(MyFeatureLibrary::class.java)
PowerMockito.doNothing().`when`<MyFeatureLibrary>(MyFeatureLibrary::class.java
, "initLibrary", Mockito.any(Activity::class.java)
, Mockito.anyBoolean() )
val sampleActivity = Robolectric.buildActivity(SampleActivity::class.java).create().get()
val expectedIntent = Intent(sampleActivity, MainActivity::class.java)
assertTrue(shadowOf(sampleActivity).nextStartedActivity.filterEquals(expectedIntent))
}
}
测试类最初没有与电源模拟相关的代码。测试将按预期进行。
现在,我必须在基本活动中进行静态方法调用。 因此,我正在尝试在BaseActivity中对模拟 initLibrary 静态方法进行调用。
我现在遇到很多错误。谷歌搜索之后,我放了很多powermock忽略包。但这并不能完全解决错误。
我希望电源模拟完全忽略与Android注入相关的类(Android.Inject(this))。 有人遇到过这个问题吗?
最新错误消息:
java.lang.RuntimeException: com.sample.myapp.TestApplication does not implement dagger.android.HasActivityInjector