Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was creating some solving algorithm and write test for it. this is the tests :

    @Test
    public void test1(){
        boolean expected = true;
        int n = 7;
        DigitIncreasing digitIncreasing = new DigitIncreasing();
        assertEquals(expected,digitIncreasing.isDigitIncreasing(n));
    }

    @Test
    public void test2(){
        boolean expected = true;
        int n =36;
        DigitIncreasing digitIncreasing = new DigitIncreasing();
        assertEquals(expected,digitIncreasing.isDigitIncreasing(n));
    }

    @Test
    public void test3(){
        boolean expected = true;
        int n = 984;
        DigitIncreasing digitIncreasing = new DigitIncreasing();
        assertEquals(expected,digitIncreasing.isDigitIncreasing(n));
    }

    @Test
    public void test4(){
        boolean expected = true;
        int n = 7404;
        DigitIncreasing digitIncreasing = new DigitIncreasing();
        assertEquals(expected,digitIncreasing.isDigitIncreasing(n));
    }

    @Test
    public void test5(){
        boolean expected = false;
        int n = 37;
        DigitIncreasing digitIncreasing = new DigitIncreasing();
        assertEquals(expected,digitIncreasing.isDigitIncreasing(n));
    }

So from here I got some idea to write the test using Table driven approach like this :

     public class testObject {
        private int input;
        private boolean expected;

        public testObject(int input, boolean expected) {
            this.input = input;
            this.expected = expected;
        }

        public int getInput() {
            return input;
        }

        public boolean isExpected() {
            return expected;
        }
    }

    public void DigitIncreasing(int input, boolean expected){
        try{
            DigitIncreasing digitIncreasing = new DigitIncreasing();
            assertEquals(expected,digitIncreasing.isDigitIncreasing(input));
        }catch (AssertionError ex){
            System.out.println("input = "+input);
            System.out.println("expected = "+expected);
            ex.printStackTrace();
        }

    }

    @Test
    public void testTable(){
        testObject[] testObjects = new testObject[]{
                new testObject(7,true),
                new testObject(36,true),
                new testObject(984,true),
                new testObject(7404,true),
                new testObject(37,false),
       };

        for (testObject test : testObjects){
            DigitIncreasing(test.getInput(),test.isExpected());
        }
    }

Using Table Driven test makes me easily add another test case. any better approach or idea? since I'm adding the catch exception to get failed test.

You can find source code here.

share|improve this question
up vote 6 down vote accepted

JUnit 4 has a Parameterized test runner that does most of the work for you. For your tests, it would look like:

import maharishi.DigitIncreasing;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class DigitIncreasingTest {

    @Parameters(name = "Test {index}: isDigitIncreasing({0})={1}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {     
              { 7, true }, { 36, true }, { 984, true }, { 7404, true }, { 37, false }
        });
    }

    private int input;

    private boolean expected;

    private DigitIncreasing digitIncreasing = new DigitIncreasing();

    public DigitIncreasingTest(int input, boolean expected) {
        input = input;
        expected = expected;
    }

    @Test
    public void test() {
        assertEquals(expected, digitIncreasing.isDigitIncreasing(input));
    }
}

This is easier to understand (in my opinion) than your formulation, where you shadow the name of the class under test, and actually runs each case as a separate test.

share|improve this answer

Just as a side note: in Java, method names start with a lowercase letter (e.g. public void digitIncreasing()) while class names start with an uppercase letter (e.g. class TestObject).

This is not enforced by the compiler but is a convention.

And: do not catch AssertionError.

share|improve this answer
1  
They have to catch the error, as they're running all cases in a single test and don't see all results otherwise. – jonrsharpe 16 hours ago
1  
But then the test doesn't fail anymore. You could only detect failure by manually inspecting System.out. – Roland Illig 15 hours ago
1  
yes, it's not a good approach. – jonrsharpe 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.