Right, so I have a MainActivity that has a view with ID fragment_container.
I now want to write a test checking that that view is being displayed.
@RunWith(AndroidJUnit4::class)
class Foo{
@get:Rule
val mainActivity = ActivityTestRule(MainActivity::class.java)
@Test fun startsWithFragmentContainerVisible(){
onView(withId(R.id.fragment_container))
.check(matches(isDisplayed()))
}
}
Ok ... that works IF the user is logged in, already.
Because if he's not, the activity will trigger
private fun startLoginCycle(){
startActivity(Intent(this,LoginActivity::class.java))
}
Which means the fragment_container is hidden behind the LoginActivity.
Here's my MainActivity.onCreate:
private lateinit var um: IUserManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
um = UserManager(this) //user manager needs a context to get access to shared preferences
/* [...] */
val au = um.activeUser
if(!au.isLoggedIn) startLoginCycle()
}
Since this test doesn't require any of the UserManager's functionality, I would like to mock the userManager and have its activeUser simply return the mock of a User that just happens to be logged in.
Can I do that? Or do I need to write a method that checks whether the login activity is visible and if so, use some testing account to log into the app?