user1611830
user1611830

Reputation: 4857

basic android unit testing

I am using Android Studio and I wrote an ultra basic test for the MAinActivity class. I inherited MainActivityTest from extends ActivityInstrumentationTestCase2<MainActivity> and I want to test

private MainActivity mMainActivityTest;
public void testPreconditions() {
        assertNotNull("mMainActivityTest is null", mMainActivityTest);
    }

using gradle unit testing, without having to launch any emulator.

When I run the test, I get no red or green color bar, and whatever I choose assertNotNull or assertNull, I always get

External tasks execution finished 'cleanTest test --tests "com.my.app.MainActivityTest.testPreconditions"

how can I do correct unit test ?

Upvotes: 0

Views: 369

Answers (1)

Evgeniy Mishustin
Evgeniy Mishustin

Reputation: 3804

In your build.gradle app level add to dependencies:

testCompile 'junit:junit:4.12'

Put your test in directory: src/test/java

Then your simple test, for example:

public class MainActivityTest(){
@Test
    public void testInitialAlwaysPasses(){
        assert true;
    }
}

Run it and you will see green lines and all the output. This is the basic Unit Test. For Instrumentation test you need to put your test in directory: src/androidTest/java. (Much easier is to right click in class you want to test and select Goto->test. It will create the test class for you). From here it will be easier to continue! Good luck!

Upvotes: 1

Related Questions