views:

29

answers:

2

I'm building a card game which is only relevant because of the examples given below.

I consider myself a fairly experienced C++ programmer, with considerable TDD experience in that language; most recently using UnitTest++. I am new to Java, and intend to target Android once I get my Java legs.

What I want to do is something akin to this UnitTest++ setup:

class DeckTest
{
public:
   Deck deck;
};

class EmptyDeck : public DeckTest
{
   // doesn't need to do anything for this simplified example
};

TEST_FIXTURE(EmptyDeck, HasNoCards)
{
   CHECK_EQUAL(0, deck.GetNumCards());
}

TEST_FIXTURE(EmptyDeck, ThrowsOnCardAccess)
{
   CHECK_THROWS(InvalidCardIndexException, deck.GetCard(0));
}

class DeckWithCards : public DeckTest
{
   void setUp()
   {
      // load deck with a bunch of cards
   }
};

TEST_FIXTURE(DeckWithCards, FirstCardIsAccessible)
{
   // ...etc.

Now, in C++ I'd just throw this all into a DeckTest.cpp file and be done; multiple fixtures all testing one client class. Makes sense to me.

However, in Java, I feel like I want to do something similar:

class DeckTester {
    Deck deck = new Deck();
}

class EmptyDeck extends DeckTester {
    @Test
    public void EmptyDeckHasNoCards() {
        assertThat(deck.GetNumCards(), equalTo(0));
    }

    @Test(expected=Deck.EmptyDeckException.class)
    public void EmptyDeckThrowsWhenGettingCard() throws Deck.EmptyDeckException {
        Card card = deck.GetCard(0);
    }
}

class DeckWithCards extends DeckTester {
    @Before
    public void AddCards() {
        Card card = new Card(Card.Type.k_missed);
        deck.AddCard(card);
        // ...or similar...
    }
}

public class DeckTests {
   // What goes here?
}

...since I can only have one public class per module I figured I'd try to build a suite or something, but I can't figure out the way to do it. I'm using the various AllTests.java in the junit-4.8.2 distro to guide me, but the one that looked most promising (org.junit.tests.AllTests) gives me compile errors when I try to mimic it.

I figured the preferred way would be to have internal classes, but junit doesn't pick those up either. It feels yucky to me to have to split these out into different files, but maybe that's just the Java way?

Tips most appreciated, thanks!

Edit: Largely I'm curious about file structure. Given the lack of response, I'm starting to think that it's not particularly feasible to have all of these things in one file. So, when developing tests like this, how do people structure them in Java? Would I create a test.deck package, with a number of test classes containing only a small number of tests, or do folks just go ahead and embrace the duplication and jam them all into one test class? (Am I giving up too easily by feeling spoiled by the ease of use of UnitTest++, where I include one file to get all of its features and where I can have a bunch of classes that test a feature in one file?)

A: 

We can create an empty class which has the runnable classes in annotation:

@RunWith(Suite.class)
@Suite.SuiteClasses({My1Test.class, My2Test.class, My2Test.class})
public class AllTests {
}
pcjuzer
Yeah that's what I thought, too. Every test gives me an error though. "Test class should have exactly one public constructor" (shouldn't the default ctor work?) and "Class My1Test should be public" (umm, the suite is public, shouldn't that be enough?). Five failures for three test methods, one for each class about the ctor, and one per test for the class being public.
dash-tom-bang
No, it isn't enough being the suite public. Test class should be also public and if you do that, .ctor problem will also disappear. Just try it.
pcjuzer
+1  A: 

Java has a limitation where only one public top level class can be in a file, so the type of style you are looking for is not quite the "java" way of doing it, but if you want that style it is more natural in TestNG, so I suggest you also consider that framework.

In JUnit, what you do is have an outer class that is a holder of all of these various classes:

@RunWith(Suite.class)
@Suite.SuiteClasses({EmptyDeck.class, DeckWithCards.class})
public class AllTests {
    public static class DeckTester {
           ///etc.
    }
    public static class EmptyDeck.class extends DeckTester {
          ///etc.

Or you can look the Enclosed.class as an alternative runner, but you would run into issues with the DeckTester since it has no tests itself.

In general, prefer composition over inheritance, so the pattern of inheriting the fixture rather than composing it gives me pause. It may be ok, but it may be too limiting in Java.

Yishai
Thanks. For whatever reason, this does not work in junit 4.8.2 but I will take a look at TestNG. I realized not long after posting my query that the inheritance was silly for such a trivial operation (creating the deck) so now I would have several fixtures that construct the deck differently, but the one-class-per-test-file apparent limitation is really dragging me down emotionally. :) I suppose I can put these test fixtures in different files, but I'm spoiled by UnitTest++ where I write a test and it runs; in JUnit it seems I have to write the test then mention it to some other system...
dash-tom-bang
@dash-tom-bang, it should work, but I have seen some funky stuff with enclosed classes. I agree that the suite running in JUnit needs some work. They have some initial stuff to address that in 4.9 (not released yet). I think you will be happier with TestNG, if you get past the XML requirements ...
Yishai
Thanks- still haven't gotten around to TestNG but I'll check it out right now... I'm not a huge fan of XML but I can suck it up for now...
dash-tom-bang
Huh wow. Ok, the articles that the TestNG author wrote pretty much expose a view on testing diametrically opposed to my own. I am not a huge fan of "all tests in a suite implicitly depend on each other," and his arguments against TestSuite are both addressed and completely ridiculous (TestSuite requires static methods? Orly?)
dash-tom-bang
Ok jeez I can't believe I failed basic reading comprehension. The 'static' modifier on the internal classes was escaping my vision until today when I went to make my first Matcher. The Matcher is still evading me but I did eventually figure out what was causing my factory to fail, it was the missing static. Coming from C++ the 'static' label on an internal class still eludes me (specifically, what does a non-static class do?) but I'm sure a bit of time with google will help me work that out.
dash-tom-bang
Oh- one note; the SuiteClasses needs to have the wrapper class's name in there too, so it's really a bit silly: `({ AllTests.EmptyDeck.class, AllTests.DeckWithCards.class })`, but it'll do for now. I still hate that I have to add that duplication, but it's better than having multiple test "scenarios" (or fixtures in the UnitTest++ vernacular) in one class.
dash-tom-bang
@dash-tom-bang, regarding the fully qualified name, you can get around that with a static import.
Yishai
@dash-tom-bang, An internal class without a static modifier is tied to an instance of the outer class. See here: http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
Yishai
@dash-tom-bang, in JUnit 3.x, suites required a static method, I think that is what it is referring to. What you get with TestNG is much more flexibility about organizing your tests. It doesn't have rules like required constructors and stuff like that - leaving it to annotations to express structure more naturally. It isn't better than JUnit in every respect (in fact, I prefer JUnit, I just roll with its organizational requirements).
Yishai
Thanks. Hadn't thought to static import the module I was in, but will try it out.
dash-tom-bang