0

I have updated my test cases dependencies from this to this(as given below in code snippet ), since then I am facing this error of java.lang.NullPointerException: Cannot run onActivity since Activity has been destroyed already . while earlier this test case was working fine.

But Now its not wokring, giving error at line scenarioRule.getScenario().onActivity(activity -> {

// this
testImplementation 'org.robolectric:robolectric:4.5-beta-1'
    testImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    testImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
    androidTestImplementation 'androidx.test:rules:1.4.0'

// to this
testImplementation 'org.robolectric:robolectric:4.14.1'
    testImplementation 'androidx.test.espresso:espresso-core:3.6.1'
    testImplementation 'androidx.test.espresso:espresso-contrib:3.6.1'
    androidTestImplementation 'androidx.test:rules:1.6.1'



@RunWith(AndroidJUnit4.class)
public class NfcCardReaderActivityTest {

    @Rule
    public ActivityScenarioRule<NfcCardReaderActivity> scenarioRule
            = new ActivityScenarioRule<>(NfcCardReaderActivity.class);

    @Test
    public void testHelperIsShown() {

            scenarioRule.getScenario().onActivity(activity -> {
                activity.onResume();

                checkHasText(
                        activity,
                        R.id.info_text,
                        R.string.helper_description
                );
            });
    }

1 Answer 1

0

So I got the solution

Basically the solution I found belongs to this link https://github.com/robolectric/robolectric/issues/8661

There was a problem with sdk 33 with respective to roboelectric. And I was testing on sdk 33

So Nfcadapter.getdefaultAdapter() was coming null

So I have to use MockStatic for NfcAdapter

try(MockedStatic<NfcAdapter> nfcMockAdapter = mockStatic(NfcAdapter.class)) {
        nfcMockAdapter.when(() -> NfcAdapter.getDefaultAdapter(any()))
                .thenReturn(mock(NfcAdapter.class));
        // Your code here
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.