Comments (3)
Please see
https://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide#Inst
rumentation_Thread_Handlers
Basically the new AndroidJUnitRunner prevents any Handler from being created on
the Instrumentation worker thread. ActivityUnitTestCase.startActivity() needs
to be called on the main thread. There are several ways to do this:
1. Use Instrumentation's runOnMainSync()
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
activity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
}
});
2. Create your own base class that extends ActivityUnitTestCase<T extends
Activity> and override startActivity
@Override
protected T startActivity(final Intent intent, final Bundle savedInstanceState,
final Object lastNonConfigurationInstance) {
return startActivityOnMainThread(intent, savedInstanceState, lastNonConfigurationInstance);
}
private T startActivityOnMainThread(final Intent intent, final Bundle
savedInstanceState,
final Object lastNonConfigurationInstance) {
final AtomicReference<T> activityRef = new AtomicReference<>();
final Runnable activityRunnable = new Runnable() {
@Override
public void run() {
activityRef.set(YourBaseActivityUnitTestCase.super.startActivity(
intent, savedInstanceState, lastNonConfigurationInstance));
}
};
if (Looper.myLooper() != Looper.getMainLooper()) {
getInstrumentation().runOnMainSync(activityRunnable);
} else {
activityRunnable.run();
}
return activityRef.get();
}
You would extend your base test class instead of ActivityUnitTestCase directly
3. If you call startActivity() in a test method and your entire test can run on
the main thread, you can simply annotate your test method with @UiThreadTest.
Hope this helps.
Original comment by [email protected]
on 28 Jan 2015 at 7:24
from android-test-kit.
Thank you very much for your response! You saved my day!
I tried all 3 techniques, from 3 > 1 > 2. At first, I was a bit bummed out
because 3 did not work. But luckily 1 & 2 was a success!
Thank you mhernan...!
Original comment by [email protected]
on 6 Feb 2015 at 10:35
from android-test-kit.
Anyone have success doing this with launchActivity as well?
Original comment by [email protected]
on 4 Mar 2015 at 4:12
from android-test-kit.
Related Issues (20)
- ActivityFinisher should wait until all finishing Activities are completely destroyed before running next test
- withText matches Buttons too HOT 1
- DexException Multiple dex files define Ljunit/extenstions/ActiveTestSuite$1; HOT 4
- withContentDescription doesn't have resId overload
- Adding "androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'" to dependencies causes build to fail HOT 3
- AndroidJUnitRunner doesn't run JUnit test with clear JUnit4 style HOT 1
- Espresso2.0 (bundled with guava) - NoSuchMethodError while initializing Espresso HOT 1
- Google maps support
- AndroidJUnit4 should skip tests with failing assumptions
- scrollTo() is not working?
- is enabled doesn't match the selected view HOT 1
- Cannot build on Maven 3.2.5
- Clicking on Component when developer don't know the ID of the component
- Espresso does not wait until SwipeRefreshLayout RefreshListener finished HOT 1
- Unable to switch on EditText overflowed by auto-complete view
- Russian text in EditText throws an exception
- Russian letter
- Unregistering idling resource causes the Espresso to think we have busy idling resources HOT 1
- Update Support Annotations version used by Espresso Contrib
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-test-kit.