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


I'm having a hard time testing, I was wondering if there is a possibility to simulate a custom return in a runtime method example:

public class TesteSmile {

    UserAndPass userAndPass = new UserAndPass();

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

    public Customizacao customizacao = new customizacao(); 

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

    public void Main() throws InterruptedException {
        DashboardNewActivity dashboardNewActivitsy = dashboard.getActivity();



        assertThat(false, customizacaoCliente.getTeste());      

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


    public void unregisterIdlingResource() throws Exception{

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

At the end of the day I do not want this assert check but rather what I said above, visual feedback. (the code is merely an example)

asked by anonymous 07.08.2018 / 19:14

0 answers