Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityUnitTestCase and startActivity

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.

like image 847
Matthias Avatar asked Nov 28 '10 15:11

Matthias


People also ask

What is startActivity method?

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.

What are instrumented tests?

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.


1 Answers

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.

like image 198
Paul Butcher Avatar answered Sep 22 '22 19:09

Paul Butcher