wL_
wL_

Reputation: 1007

What's the best way to obtain an activity object for unit testing in Android?

I need a generic activity object to test another class.

Upvotes: 1

Views: 97

Answers (2)

Joe Malin
Joe Malin

Reputation: 8641

It's hard to get a handle to an Activity other than the activity under test in a unit test based on ActivityInstrumentationTestCase2.

However, as the other respondent said, it's not a great idea to make a class dependent on a fundamental Android component like Activity. What's the use case?

Upvotes: 1

Joel Martinez
Joel Martinez

Reputation: 47759

Just make a mock ... inherit from Activity, and override whatever you need to override.

better yet, decouple the other class so that you don't have such a dependency on a framework class like Activity.

Upvotes: 1

Related Questions