1

I am working on my interview prep and the way organize my work is that each question gets its own class. This is convenient because I can group the problem statement, implementation and testing into one place making it easy to relook at problems in the future. However, I have been trying to find a way to group test cases together within that single class. If I were splitting the testing and the implementation into separate classes I could just set up an additional test suit class like this.

In my case, I have two methods for checking if a tic tac toe board is complete. One is for only 3 by 3 boards and the other is generic for N by N boards. I have a method for testing them both and a lot of "basic" tests:

    public void doTestBasic(char expected, char[][] board){
        assertThat("Only 3 by 3", expected, is(checkForWinner3By3(board)));
        assertThat("Generic", expected, is(checkForWinnerGeneric(board)));
    }

    @Test
    public void testSimpleRow(){
        char[][] board = {
                {'X','X', 'X'},
                {' ',' ', ' '},
                {' ',' ', ' '}};
        char expected = 'X';
        doTestBasic(expected, board);
    }

    @Test
    public void testSimpleCol(){
        char[][] board = {
                {'X',' ', ' '},
                {'X',' ', ' '},
                {'X',' ', ' '}};
        char expected = 'X';
        doTestBasic(expected, board);
    }

    @Test
    public void testBasicTie(){
        char[][] board = {
                {'X','O', 'O'},
                {'O','X', 'X'},
                {'O','X', 'O'}};
        char expected = 'N';
        doTestBasic(expected, board);
    }

And then I more tests for only larger boards like:

    @Test
    public void testHigherEmpty(){
        char[][] board = {
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '}};

        char expected = 'N';
        assertEquals(expected, checkForWinnerGeneric(board));

    }


    @Test
    public void testHigherRow(){
        char[][] board = {
                {'X','X', 'X', 'X'},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '}};

        char expected = 'X';
        assertEquals(expected, checkForWinnerGeneric(board));

    }

I want the two different kinds of tests to appear in two different groups when run instead of one long list, all while staying inside the one class file for the problem. Note that I am using Java 8 and JUnit4.

ecp89
  • 25
  • 4

1 Answers1

1

You can call your test in a function that represent a group :

@Test
public void simpleTests() {
    testSimpleRow();
    testSimpleCol();
    testBasicTie();
}

@Test
public void higgerTests() {
    testHigherRow();
    testHigherCol();
}

This way you are going to execute each other method in a bigger test method.

Don't forget to remove @Test annotation on other tests.

Anthony Raymond
  • 6,225
  • 5
  • 38
  • 55
  • 1
    Since your problem seems to be solved, you can mark it as `solved` by pressing the green icon at the left of the most helpfull answer. – Anthony Raymond Dec 28 '15 at 19:51
  • I am not a junit user,but the solution seems to be resonable, I will push up for you. – Gang Dec 29 '15 at 13:08