Пытаясь начать реализацию тестирования для моего класса приложений, который инициирует кинжал и создает экземпляр appComponent с AppModule и модулем хранения, я обнаружил, что расширение ApplicationTestCase должно помочь и настроить приложение, но проблема getApplication () всегда возвращает нуль.Это правильный способ использовать объект приложения в JUnit и почему getApplication()
возвращает null
?
class LirisApplication : Application() {
companion object {
lateinit var appComponent: AppComponent
}
fun getDataComponent(): AppComponent {
return appComponent
}
override fun onCreate() {
super.onCreate()
initializeDagger()
Fabric.with(this, Crashlytics())
}
private fun initializeDagger() {
appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.storageModule(StorageModule())
.networkModule(NetworkModule(BuildConfig.BASE_URL))
.build()
}
}
ApplicationTest
public class ApplicationTest extends ApplicationTestCase<LirisApplication> {
private LirisApplication application;
public ApplicationTest() {
super(LirisApplication.class);
}
protected void setUp() throws Exception {
super.setUp();
createApplication();
application = getApplication();
}
public void testCorrectVersion() throws Exception {
PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
assertNotNull(info);
MoreAsserts.assertMatchesRegex("\\d\\.\\d", info.versionName);
}
}
Ошибка в этой строке, потому что приложение пустое
PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
Журнал
java.lang.NullPointerException
at digitu.com.osmos.ApplicationTest.testCorrectVersion(ApplicationTest.java:27)
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 junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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)