Peter
Peter

Reputation: 2500

How to test element of custom sdk android unit test

I've started learning android unit tests, but it looks very hard to find some good guides or information. Every example have a stupid example about 2+2 = 4

Say I write a little SDK which has few functions

MySdk.Init(Context context)
MySdk.CallTask()

I create an androidTest file

How should I call my SDK functions to check how they work? Somewhere required parameters like int/string/context. I just really don't understand, please help me.

This is what I've tried

public class AndroidTest {
    private Activity context;


//default test
@Test
public void addition_correct() throws Exception {
    assertEquals(4, 2 + 2);
}

@Test
public void checkContext() {
    context = getActivity();
    assertNotNull(context);
}

@Test
public void testInitPhase() {

    MySdk.Init(context, new SdkInitializationListener() {
        @Override
        public void onInitializationSuccessful(String adv_id) {
            assert (adv_id != null);
        }

        @Override
        public void onInitializationError() {

        }
    });
}

}

For context i was tried context = new mockContext();. It's passed as context = null and my SDK failed with initialization.

Upvotes: 1

Views: 460

Answers (1)

Michael Dodd
Michael Dodd

Reputation: 10270

Unit tests are mainly about testing an individual class in isolation, so that you can check if individual public methods of a class behave as you intend them to, and continue to do so if you change that class' code in the future. Let's say you have a class like this:

public class UtilityFunctions {
    public int double(int value) {
        return value * 2;
    }

    public String mirror(String value) {
         if (value == null) return "";
         return value + new StringBuilder(value).reverse().toString();
    }
}

You want to test these two methods with:

  • valid input values, and check the output is as expected
  • invalid values, and check that errors are handled accordingly (and the correct exceptions thrown if necessary)

So a test class for the above class may look like this

@RunWith(JUnit4.class)
public class UtilityFunctionsTest {
    private UtilityFunctions utility;

    @Before
    public void setUp() {
        // Initialises any conditions before each test
        utility = new UtilityFunctions();
    }

    @Test
    public void testDoubleFunction() {
         assertEquals(2, utility.double(1));
         assertEquals(8, utility.double(4));
         assertEquals(-12, utility.double(-6));
         assertEquals(0, utility.double(0));
    }

    @Test
    public void testMirror() {
         assertEquals("", utility.mirror(null));
         assertEquals("", utility.mirror(""));
         assertEquals("aa", utility.mirror("a"));
         assertEquals("MirrorrorriM", utility.mirror("Mirror"));
    }
}

These standard Java unit tests are run from the test directory. However, you'll need to run tests in the androidTest directory whenever you're using Android-specific classes such as Context. If you're creating a MockContext, you're simply creating an empty Context whose methods don't do anything.

Without me knowing anything about what your MySDK does, I think you may need to pass a fully-functioning Context into your class for your tests. The Android JUnit runner does provide this with InstrumentationRegistry.getTargetContext(), so for your example, you may need to add this @Before method:

@Before
public void setUp() {
    context = InstrumentationRegistry.getTargetContext();
}

You'll also need to remove the context = getActivity(); line from your first test.

Upvotes: 2

Related Questions