Junit 4 runner in android test project “Sample Pro

2019-09-01 15:33发布

问题:

Provide me Android sample test project with only 2-3 java files,by using JUnit 4. I was trying to do this.

My main test file.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
SimpleMathTest.class
// add other classes here (comma separated)
})
public class TestSuite {
}

and I made another test file as JUnit 4 that is

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
public class SimpleMathTest {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }
    @Before
    public void setUp() throws Exception {
    }
    @After
    public void tearDown() throws Exception {
    }
}

but at the time of execution Error displays:

SampleTest: Failed to launch test

please give me some solution.

Thanks

回答1:

The following steps worked for me:

  1. Create new JUnit (and NOT "Android Junit Test") Testconfiguration:
  2. Add a new test launcher:
  3. Choose the following launcher:

With this configuration I can run my unit tests. They run on the pc and not on the phone. Hope this helps.



回答2:

You don't have any @Test methods.

Add:

@Test
public void test()  {
    //Test code here
}