Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda