To serve ExampleUnitTest and ExampleInstrumentedTest in Android Studio?

2

Hello, I'm learning to develop on Android and would like to know the usefulness of these files and how Android uses them.

I noticed that some applications sometimes do not have these files.

    
asked by anonymous 10.01.2018 / 16:25

1 answer

3

These are test classes. ExampleUnitTest is for unit tests, they are local tests and do not need an emulator or device to run. You can test methods in this class, as well as create new unit test classes. Example:

@Test
public void get_current_user_test() {
    User mUser = new User();
    mUser.setName("joao");
    mUser.setEmail("[email protected]");
    mUser.setUserId("123");
    User mUser_two;
    UserDao userDao = new UserDao();
    mUser_two = userDao.getCurrentUser("joao", "[email protected]", "123");

    assertEquals(mUser.getEmail(), mUser_two.getEmail());
    assertEquals(mUser.getName(), mUser_two.getName());
}

This method above tests whether the user you created is the same as the user who is in a class in your project for example.

ExampleInstrumentedTest is class for instrumental testing, and again, you can create your own classes. This type of test needs an emulator or device and in the background it installs and runs your app. It serves for UI testing, but can also be used to test context and dependencies.

Example:

Context mMockContext;
SharedPreferences preferences;

@Before
public void setup() {
    mMockContext = InstrumentationRegistry.getTargetContext();
    preferences = mMockContext.getSharedPreferences(Contract.SETTINGS_PREF, 0);
}

@Test
public void test_encode_string() {
    String from = "<p>htmltext</p>";
    String to = "htmltext\n\n";
    from = NewsFeedDao.encodeString(from);

    Assert.assertEquals(from, to);
}

In this method, we test the Encoding of a text string retrieved from a news feed from the XPTO site.

    
10.01.2018 / 16:39