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);
}
}
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.