In the JavaDoc of ActivityUnitTestCase it says:
Do not call from your setUp() method. You must call this method from each of your test methods.
Isn't putting something in every test method equivalent to putting it in setUp
, considering that the whole idea behind that method is to do just that, i.e. executing something before every test?
Also, why are we not allowed to do that? I tried it, and it works just fine.
To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.
It appears that setUp runs with the test project's class loader, while the actual test methods run with the app under test's class loader. See, for example, this discussion on the RoboGuice mailing list:
http://groups.google.com/group/roboguice/browse_thread/thread/2e129f87ead10b10
Why this is the case, I'm not sure (it seems like a very strange design decision to me). But the upshot is that you can't access anything in the app under test in your setUp method. Which moves setUp into chocolate teapot territory.
Note that this restriction does not apply if you're testing a library project as described here:
http://www.paulbutcher.com/2010/09/android-library-project-with-tests-step-by-step/
Because in that case the tests and the code under test are all in a single app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With