How to make a mock in an android class at runtime?

Asked

Viewed 207 times

4

I am having a certain difficulty in the tests, I wanted to know if there is a possibility to simulate a custom return in a method at runtime example:

@RunWith(JUnit4.class)
public class TesteSmile {

    UserAndPass userAndPass = new UserAndPass();

    @Rule
    public ActivityTestRule<Exemplo> dashboard = new ActivityTestRule<>(Exemplo.class);

    @Mock
    public Customizacao customizacao = new customizacao(); 


    @Before
    public void setUp() throws Exception {
        userAndPass = new UserAndPass();    
    }


    @org.junit.Test
    public void Main() throws InterruptedException {
        DashboardNewActivity dashboardNewActivitsy = dashboard.getActivity();

        customizacaoCliente.setTeste(true);

        when(customizacaoCliente.getTeste()).thenReturn(false);

        assertThat(false, customizacaoCliente.getTeste());      


        onView(withText("Item 1")).perform(click());

    }

    @After
    public void unregisterIdlingResource() throws Exception{
        //Intents.release();
    }
}

this is just an example, which I did will not post everything, but just to have basis, I mocked a class, in this class I simulated a false return, and in the assert it passes the test, because it returns the answer I wanted in the call of get, the whole problem is, I’m doing visual tests on the application and validating fields in general, but this mock only drives the local instance, the one I created inside the test, is there any way to simulate a fake response in the running class? test runs on android studio emulator, opens the screens, tests the fields, validates errors using onview, more in a certain location I want to bring other elements, but this on the visual screen(ui) on the emulator run... there is a way to do this without directly interfering with the class code?

Ultimately I don’t want this assert check but rather what I said above, the visual return. (The code is merely an example)

  • If you are performing instrumented tests, you should exchange @RunWith(JUnit4.class)for @RunWith(AndroidJUnit4.class)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.