0

I have this jUnit test class

public class TestRaavareBatch {

    @Before
    public void prep(){
        try { new Connector(); } 
        catch (InstantiationException e) { e.printStackTrace(); }
        catch (IllegalAccessException e) { e.printStackTrace(); }
        catch (ClassNotFoundException e) { e.printStackTrace(); }
        catch (SQLException e) { e.printStackTrace(); }
    }

    MySQLRaavareBatchDAO rvb = new MySQLRaavareBatchDAO();

    @Test
    public void testgetRaavareBatch() throws DALException{
        RaavareBatchDTO rvbOBJ = rvb.getRaavareBatch(7);
        assertEquals(7, rvbOBJ.getRaavareId());
        assertEquals(100.0, rvbOBJ.getMaengde(),0.0);
        assertEquals(7, rvbOBJ.getRbId());
    }
    @Test
    public void testgetRaavareBatchList() throws DALException{
        List<RaavareBatchDTO> rvbOBJ = rvb.getRaavareBatchList();
        assertEquals(rvbOBJ.size(), 8);
    }
    @Test
    public void testgetRaavareBatchListId() throws DALException{
        List<RaavareBatchDTO> rvbOBJ = rvb.getRaavareBatchList(5);
        assertEquals(rvbOBJ.size(), 2);
    }
    @Test
    public void testcreateRaavareBatch() throws DALException{
        RaavareBatchDTO test;
        rvb.createRaavareBatch(test = new RaavareBatchDTO(8, 8, 200.0));
        RaavareBatchDTO rvbOBJ = rvb.getRaavareBatch(8);
        assertEquals(8, rvbOBJ.getRbId());
        assertEquals(200.0, rvbOBJ.getMaengde(),0.0);
        assertEquals(8, rvbOBJ.getRbId());
    }
    @Test
    public void testupdateRaavareBatch() throws DALException{
        RaavareBatchDTO test;
        rvb.updateRaavareBatch(test = new RaavareBatchDTO(8, 7, 100.0));
        RaavareBatchDTO rvbOBJ = rvb.getRaavareBatch(8);
        assertEquals(7, rvbOBJ.getRaavareId());
        assertEquals(100.0, rvbOBJ.getMaengde(),0.0);
    }
}

It connects to a database with 7 rows, and after i run the last test "updateRaavareBatch" i have created a new row so the size of the list in testgetRaavareBatchList() will be 8. But it gives me an error because it counts the size before i create a new row.. How can i run testgetRaavareBatchList() after i create the new row and update it.

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Muddi
  • 39
  • 8
  • 1
    don't. if I'm not mistaken, there's a rollback after each test. if you want to verify that one extra line is in the db, you'll need to put the insert in the same test as the check for the extra line. – Stultuske May 05 '15 at 13:36
  • 11
    If your tests interfere with each other, you should fix that so that they *don't* interfere with each other, rather than just trying to control the order. – Jon Skeet May 05 '15 at 13:36
  • It is not occasionally called *unit* testing. If some tests depends on others, join them into one `@Test`. – Alex Salauyou May 05 '15 at 13:51
  • Each test should be self sufficient and should able to run independently. Ordering of test execution can make the maintenance of your test code difficult. – Manmay May 05 '15 at 15:20

2 Answers2

3

I once got something like that in testing queries, insertions and deletions in a database.

I ended with the following infra in order to ensure test independance :

  • prepare the database connection in a @Before method
  • rollback in @After
  • put some inserts in private not @Test annotated utility methods to avoid duplication
  • when needed the @Test methods called utility methods, and did their job with assertions

In another harder case, I created an embedded database in a @BeforeClass method and destroyed it in @AfterClass

But you should never rely on test order.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

You can use @FixedMethodOrder annotation on your test class.

A simple example is the following:

import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters;

//Running test cases in order of method names in ascending order
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTestCasesExecution {
    @Test
    public void secondTest() {
        System.out.println("Executing second test");
    }

    @Test
    public void firstTest() {
        System.out.println("Executing first test");
    }

    @Test
    public void thirdTest() {
        System.out.println("Executing third test");
    }
}

Output:

Executing first test
Executing second test
Executing third test 

Just one thing about your particular test scenario though. It is better in your case to have a proper @Before and @After methods to setup and rollback database tests. Later on, if your codebase is big enough you might run into cases where one test does not clean up properly and makes another random testcase fail.

References:

Simple TestCase source

JUnit Javadoc for the @FixMethodOrder

Another decent page on JUnit

Shiraaz.M
  • 3,073
  • 2
  • 24
  • 40