JUnit 4 Test Suite - addTest not applicable?

Discussion in 'Java' started by TheSouthLondonSlasher, Sep 24, 2008.

  1. 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.
     
    TheSouthLondonSlasher, Sep 24, 2008
    #1
    1. Advertising

  2. TheSouthLondonSlasher

    Arne Vajhøj Guest

    TheSouthLondonSlasher wrote:
    > 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
     
    Arne Vajhøj, Oct 6, 2008
    #2
    1. Advertising

  3. TheSouthLondonSlasher

    Arne Vajhøj Guest

    Arne Vajhøj wrote:
    > TheSouthLondonSlasher wrote:
    >> 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 }).


    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
     
    Arne Vajhøj, Oct 6, 2008
    #3
  4. TheSouthLondonSlasher

    jbo Guest

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

    regards
    -Jarek

    > @RunWith(Suite.class)
    > @SuiteClasses({JU1.class,JU2.class})
     
    jbo, Oct 7, 2008
    #4
  5. TheSouthLondonSlasher

    Arne Vajhøj Guest

    jbo wrote:
    > 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
     
    Arne Vajhøj, Oct 8, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith
    Replies:
    1
    Views:
    517
    Keith
    Feb 8, 2004
  2. Tristan
    Replies:
    1
    Views:
    423
    Hugo Pragt
    Jan 26, 2004
  3. Ninan
    Replies:
    4
    Views:
    393
    Howard
    Jun 29, 2006
  4. ankur
    Replies:
    1
    Views:
    12,988
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  5. D. Krmpotic
    Replies:
    4
    Views:
    246
    NAKAMURA, Hiroshi
    Aug 3, 2007
Loading...

Share This Page