Category : unit-testing

I’m trying to upgrade org.powermock:powermock-api-mockito2 version from 2.0.0 to 2.0.9 in an existing working Unit Test setup, but PowerMockito throws below exception on ‘thenReturn’ method : java.lang.ClassCastException: org.powermock.api.mockito.invocation.MockitoMethodInvocationControl cannot be cast to org.powermock.api.mockito.invocation.MockitoMethodInvocationControl These are the other test dependencies I use : testImplementation ‘junit:junit:4.13.2’ testImplementation ‘androidx.test:core:1.4.0’ testImplementation "org.robolectric:robolectric:4.3.1" testImplementation ‘org.powermock:powermock-core:2.0.9’ testImplementation ‘org.powermock:powermock-module-junit4:2.0.9’ testImplementation "org.powermock:powermock-module-junit4-rule:2.0.0" testImplementation ..

Read more

I’m try to testing a viewmodel stateflow implementation using Turbine library. This is my implementation. @HiltViewModel class CountriesViewModel @Inject constructor( application: Application, private val state: SavedStateHandle, private val countryUseCase: CountryUseCase ) : AndroidViewModel(application) { val filterState: MutableStateFlow<FilterType> = MutableStateFlow(state.get(FILTER_KEY) ?: FilterType.Empty) val stateFlow: StateFlow<MainViewState> = flow { filterState.collect { filter -> val useCase = if ..

Read more

I am getting IllegalStateException when a getString() is called inside a Fragment Class Caused by: java.lang.IllegalStateException: Fragment SomeFragment{1df37025} (29efd1d0-77c1-4293-a0f6-b808c7559cf4)} not attached to a context. at androidx.fragment.app.Fragment.requireContext(Fragment.java:805) at androidx.fragment.app.Fragment.getResources(Fragment.java:869) at androidx.fragment.app.Fragment.getString(Fragment.java:891) SomeFragment Code public void someMethod() { getString(R.string.some_res) } Test Code @Before fun setUp() { val activity = Robolectric.buildActivity(SomeActivity::class.java).create().get() fragment = SomeFragment() fragmentManager = activity.supportFragmentManager val ..

Read more

I’m trying to use Unit testing using JUnit in my android project, so I tried to run the ExampleUnitTest class in the (test) folder with this statement: Assert.assertEquals(4,5) and it passed I don’t know why then I created another class in the same folder and now I get the error even on the ExampleUnitTest class ..

Read more

I have a GroundBaseView class which extends RelativeLayout. This GroundBaseView class has cardContainer member variable in which it stores reference from a XML framelayout, and also adds addOnLayoutChangeListener which gets triggered whenever there is a change in the it’s layout. I have overridden the method onLayoutChange inside it and have one condition in which whenever ..

Read more

I have to setup @Before in gradle 7.0.2 and gradle api 7.0.2 for my below code but setup() function is not callable. import org.gradle.internal.impldep.org.junit.Before class CodeCoveragePluginForAllTest : BaseTest() { @Before fun setup() { testProjectDir.apply { createGradleProperties() createSettingsFile(projectName = TEST_PROJECT_NAME) buildFile = createProjectFile("build.gradle") { """ buildscript { repositories { google() mavenCentral() } dependencies { classpath ‘com.android.tools.build:gradle:7.0.2’ ..

Read more

I’ve implemented a Repositoryclass like this: class Repository @Inject constructor(private val dao: Dao) { val response: LiveData<List<MyObject>> = dao.getAllObjects() … } Now I wrote a simple unit test for that peace of code like this: class RepositoryTest { @MockK private lateinit var mockLiveData: LiveData<List<MyObject>> @MockK private lateinit var mockDao: Dao @InjectMockKs private lateinit var repository: ..

Read more

When trying to run this test fails because of Wanted but not invoked for fetchForecastFiveDays function and this is what i have in view model and repository. ForecastViewModelTest.kt class SingleNetworkCallViewModelTest { @get:Rule val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule() @get:Rule val testCoroutineRule = TestCoroutineRule() @Mock private lateinit var forecastObserver: Observer<Resource<ForecastResponse>> @Mock private lateinit var forecastResponse: ForecastResponse @Mock ..

Read more