JUnit 4 Test Suite - addTest not applicable?

  • Thread starter TheSouthLondonSlasher
  • Start date
T

TheSouthLondonSlasher

I have a simple JUnit4 test, which I want to run from a testSuite,
since obviously, later we'll have many tests. So my test is:

package test.gg.ba.util;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.gg.ba.util.Utility;

public class UtilityTest {

@Test
public void testCheckNull() {
assertEquals(Utility.checkNull("Ham"), false);
}
}

This is fine and dandy, and works well when I run as JUnit Test in
Eclipse. However, the Suite, I'm having trouble with:

package test;

import test.gg.ba.util.UtilityTest;
import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
//$JUnit-BEGIN$

suite.addTest(test.gg.ba.util.UtilityTest.class);

//$JUnit-END$
return suite;
}
}

The line suite.addTest tells me:
The method addTest(Test) in the type TestSuite is not applicable for
the arguments (Class <UtilityTest>).

I understand what this message means, but all of the examples I find
do it this way, so I do not understand what the mistake I am making
is.

Any help will be greatly appreciated! Thank you in advance.
 
A

Arne Vajhøj

TheSouthLondonSlasher said:
I have a simple JUnit4 test, which I want to run from a testSuite,
since obviously, later we'll have many tests. So my test is:

package test.gg.ba.util;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.gg.ba.util.Utility;

public class UtilityTest {

@Test
public void testCheckNull() {
assertEquals(Utility.checkNull("Ham"), false);
}
}

This is fine and dandy, and works well when I run as JUnit Test in
Eclipse. However, the Suite, I'm having trouble with:

package test;

import test.gg.ba.util.UtilityTest;
import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
//$JUnit-BEGIN$

suite.addTest(test.gg.ba.util.UtilityTest.class);

//$JUnit-END$
return suite;
}
}

The line suite.addTest tells me:
The method addTest(Test) in the type TestSuite is not applicable for
the arguments (Class <UtilityTest>).

I understand what this message means, but all of the examples I find
do it this way, so I do not understand what the mistake I am making
is.

You code above is all JUnit 3 code.

Remove all imports of junit.framework.* and use annotations.

The relevant one is:

@SuiteClasses( { Utility.class }).

Arne
 
A

Arne Vajhøj

Arne said:
You code above is all JUnit 3 code.

Remove all imports of junit.framework.* and use annotations.

The relevant one is:

@SuiteClasses( { Utility.class }).

Let me give an example.

First two test classes:

import static org.junit.Assert.*;

import org.junit.Test;

public class JU1 {
@Test
public void test1() {
assertNotNull("ABC");
}
}

and

package october;

import static org.junit.Assert.*;

import org.junit.Test;

public class JU2 {
@Test
public void test1() {
assertNotNull("ABC");
}
}

and now a suite:

package october;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({JU1.class,JU2.class})
public class AllJU {
}

Arne
 
J

jbo

Hi !
Should one manually list test classes in @SuiteClasses ?
Is there no supprt from Eclipse plugin?

regards
-Jarek
 
A

Arne Vajhøj

jbo said:
Should one manually list test classes in @SuiteClasses ?
Is there no supprt from Eclipse plugin?

I don't know.

But considering that:
1) it is perfectly valid to have a suite that only run some
of the tests in the package
2) in JUnit 3.x you would need to specify the classes
in the code
then I really doubt there are any magic in Eclipse.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top