Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
mocking ``lateinit var``s

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?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!