Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringJUnit4ClassRunner with JUnit testsuite error

Tags:

junit

spring

ant

I am trying to setup Junit test suite with Spring for the first time and tried with couple of changes in my classes, but no luck and ended up with this error : "junit.framework.AssertionFailedError: No tests found in Myclass"

Briefly, I have 2 test classes both are from same base class which loads Spring context as below

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations =
{
"classpath:ApplicationContext.xml"
})

I tried adding those 2 test classes into a suite as below

@RunWith( SpringJUnit4ClassRunner.class )
@SuiteClasses({ OneTest.class, TwoTest.class })
public class MyTestSuite extends TestCase {

//nothing here
}

I am running this test suite from ant. But, this gives me an error saying "No tests found" However, If I run the individual 2 test cases from ant, they work properly. Not sure why is this behaviour, I am sure missing something here. Please advice.

like image 223
San Avatar asked Sep 14 '12 18:09

San


1 Answers

As mentioned in the comments, we run the TestSuite with @RunWith(Suite.class) and list all the test cases with @SuiteClasses({}). In order to not repeat the @RunWith(SpringJunit4ClassRunner.class) and @ContextConfiguration(locations = {classpath:META-INF/spring.xml}) in each test case, we create an AbstractTestCase with these annotations defined on it and extend this abstract class for all test cases. A sample can be found below:

/**
 * An abstract test case with spring runner configuration, used by all test cases.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{ "classpath:META-INF/spring.xml" })
public abstract class AbstractSampleTestCase
{
}


public class SampleTestOne extends AbstractSampleTestCase
{
    @Resource
    private SampleInterface sampleInterface;

    @Test
    public void test()
    {
        assertNotNull(sampleInterface);
    }

}


public class SampleTestTwo extends AbstractSampleTestCase
{
    @Resource
    private SampleInterface sampleInterface;

    @Test
    public void test()
    {
        assertNotNull(sampleInterface);
    }

}


@RunWith(Suite.class)
@SuiteClasses(
{ SampleTestOne.class, SampleTestTwo.class })
public class SampleTestSuite
{
}

If you don't want to have an AbstractSampleTest, then you need to repeat the spring runner annotations on each test case, until Spring comes up with a SpringJunitSuiteRunner similar to how they need to add a SpringJunitParameterizedRunner.

like image 116
Vikdor Avatar answered Sep 22 '22 14:09

Vikdor