Writing in the front

There are plenty of articles on the web about how Android unit tests are configured, and I won’t repeat them here.

But unit testing is more than just configuration. How to actually test some real points is the focus of this document.

How do I check if an Activity starts another Activity with startActivity

// Robolectric version is currently used: 4.2

ShadowActivity shadowActivity = Shadows.shadowOf(activity);

/ /.. Perform startForActivity..

Intent intent = shadowActivity.peekNextStartedActivityForResult().intent;
assertEquals(intent.getComponent().getClassName(), NextActivity.class.getName());
// Further tests can be performed on data in the intent, which is skipped here
Copy the code

How do YOU simulate the Activity lifecycle

// Launch, the default is create -> start -> resume, and the RESUMED is RESUMED
They are DESTROYED if you finish the launch in onCreate
try(ActivityScenario<YourActivity> scenario = ActivityScenario.launch(YourActivity.class)) { scenario.onActivity(activity - > {/ / pause execution
				scenario.moveToState(Lifecycle.State.STARTED);

				/ / stop execution
				scenario.moveToState(Lifecycle.State.CREATED);

				/ / perform to destroy
				scenario.moveToState(Lifecycle.State.DESTROYED);
			});
		}
Copy the code

How to pass parameters to an Intent when launching an Activity

Intent launchIntent = new Intent(ApplicationProvider.getApplicationContext(), YourActivity.class);
launchIntent.putExtra("key"."value");
try (ActivityScenario<YourActivity> scenario = ActivityScenario.launch(launchIntent)) {
			scenario.onActivity(activity -> {
				// blablabla
			});
		}
Copy the code

How to mock a static method using Mockito

Mockito after 3.5.0, it was possible to mock static methods by relying on mockito-inline

Javadoc. IO/doc/org. Moc…

try (MockedStatic<YourClass> mockYourClassStatic = Mockito.mockStatic(YourClass.class)) {
  mockYourClassStatic.when(() -> YourClass.staticMethod()).thenReturn("mockResult");
  // This can also be verified
  mockYourClassStatic.verify(() -> YourClass.staticMethod2());
}
Copy the code

Note that this scope is limited to the current thread only

How do I replace a singleton with a mock or spy

Typically, we’ll replace the singleton with a mock object so that it can return the desired value later in the program

Alternatively, we’ll replace it with a Spy object, which can be used for Verify or partial mocks

Mock getInstance (spy); // Mock getInstance (spy)
YourClass yourClass = YourClass.getIntance();
YourClass spyYourClass = Mockito.spy(yourClass);
MockedStatic<YourClass> mockStaticYourClass = Mockito.mockStatic(YourClass.class);
mockStaticYourClass.when(() -> YourClass.getIntance()).thenReturn(spyYourClass);

// To return a mock, just return the mock object in thenReturn
Copy the code

How do I delay unit tests

There is a case, there is a button on the click event happens, did shake the processing, the way he used is called SystemClock. ElapsedRealtime () to get the time, and last time to exceed the threshold can point.

Therefore, in this case, when the interface starts up, there is actually a very short time, and the button cannot be clicked, but the user cannot touch this situation.

UnitTest can be timed using Robolectric, using the following code

Robolectric.getForegroundThreadScheduler().advanceTo(1500);
Copy the code

How to make RxJava asynchronous to synchronous

Here we use RxJava 1, EMMM

RxJavaPlugins.getInstance().registerSchedulersHook(new RxJavaSchedulersHook() {
	@Override
	public Scheduler getIOScheduler(a) {
		returnSchedulers.trampoline(); }}); RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
	@Override
	public Scheduler getMainThreadScheduler(a) {
		returnSchedulers.immediate(); }});Copy the code

How do I get arguments passed in using Mockito mock methods

when(yourMock.func(any(), any())).then(new Answer<Object>() {
			@Override
			public Object answer(InvocationOnMock invocation) throws Throwable {
				int arg1 = invocation.getArgument(0);
				String arg2 = invocation.getArgument(1);
				return null; }});Copy the code

How to mock a method that returns void using Mockito

doAnswer(answer).when(mockObject).voidMethod();
Copy the code

(To be continued)