Issue
I'm trying to pass the context into a constructor for a unit test. And I'm getting the following error
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
I've tried the following code.
public class Handler {
@Mock
MyViewModel viewModel;
@Before
public void setup() {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
Handler = new Handler(context, viewModel);
}
}
Solution
Add @RunWith annotation with AndroidJUnit runner.
@RunWith(AndroidJunit4::class)
public class Handler {
@Mock
MyViewModel viewModel;
@Before
public void setup() {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
Handler = new Handler(context, viewModel);
}
}
Also add the following dependency if its not addded in the build.gradle(app)
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
Answered By - Rahul Singhal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.