Hi everyone,

I'm having a problem with a JUnit test I've written. I think there should be a simple solution but I can't work out what's wrong. I'm new to using JUnit so any advice would be a big help.

Here's the test class below. At the moment I'm just testing that 3 arraylists are of the correct size (100 elements). The 1st test passes but the 2nd and 3rd fail, with the results saying that they have 200 and 300 elements respectively. So it seems the results of arraylist.size() are accumulating, which is strange because they're in different tet methods.

Ps. I have put println calls in the actual methods being tested and I know they have the right number of elements (all have 100 each).

public class DataBuilderTest
{

    DataBuilder db = new DataBuilder();
    private int expectedSize = 100;

    public DataBuilderTest()
    {
    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {
        System.out.println("** DataBuilder test start");
    }

    @AfterClass
    public static void tearDownClass() throws Exception
    {
        System.out.println("** DataBuilder test end");
    }

    @Before
    public void setUp()
    {
    }

    @After
    public void tearDown()
    {
    }

    @Test
    public void testGetCharacters()
    {
        ArrayList<String> characters = DataBuilder.getCharacters();
        assertEquals(expectedSize, characters.size());
    }

    @Test
    public void testGetMeanings()
    {
        ArrayList<String> meanings = DataBank.getMeanings();
        assertEquals(expectedSize, meanings.size());
    }

    @Test
    public void testGetReadings()
    {
        ArrayList<String> readings = DataBank.getReadings();
        assertEquals(expectedSize, readings.size());
    }
}

Any ideas what's up with the above test code?

Have you tried printing out the values being tested before the tests?
System.out.println("characters.size()=" + characters.size() + " vs eS=" + expectedSize);

FOr all three arrays after their references are set?

Interesting. I tried your suggestion NormR1, but the print statement is only printed for the 1st array, not for the 2nd or 3rd. I didn't notice that before.

Here's the test output just to clarify:

** DataBuilder test start
retrieveKanji()
characters.size()=100 vs eS=100
** DataBuilder test end

So for some reason the 2nd and 3rd tests are failed yet never executed, is that right?

Looks like its time for more debugging.
If the println()s didn't print, then they probably weren't executed.

okay. Thanks for the clue though. Much appreciated.

This doesn't seem to be a problem with JUnit code but more with the way data is retrieved and its size calculated. Are you sure the DataBank and DataBuilder classes are not holding some sort of global state?

Thanks for the suggestion SOS!

How do you implement these methods of these classes -- DataBuilder.getCharacters(), DataBank.getMeanings(), and DataBank.getReadings()?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.