Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda