11
\$\begingroup\$

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 codehere.

askedDec 11, 2016 at 9:19
Gujarat Santana's user avatar
\$\endgroup\$
3
  • \$\begingroup\$Side note, how are the digits in984 and7404 increasing?\$\endgroup\$CommentedDec 13, 2016 at 15:15
  • \$\begingroup\$I'm kinda forget, but here is the algorithmgithub.com/Gujarats/SolvingProblem/blob/master/src/main/…\$\endgroup\$CommentedDec 14, 2016 at 6:19
  • 1
    \$\begingroup\$Ah I sees... it's not only looking at the digits, but performs some multiplication based on the number of digits to see if it equals the input or not.\$\endgroup\$CommentedDec 14, 2016 at 15:44

3 Answers3

13
\$\begingroup\$

JUnit 4 has aParameterized 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.

answeredDec 11, 2016 at 9:45
jonrsharpe's user avatar
\$\endgroup\$
1
2
\$\begingroup\$

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 catchAssertionError.

answeredDec 11, 2016 at 9:51
Roland Illig's user avatar
\$\endgroup\$
3
  • 1
    \$\begingroup\$They have to catch the error, as they're running all cases in a single test and don't see all results otherwise.\$\endgroup\$CommentedDec 11, 2016 at 10:03
  • 1
    \$\begingroup\$But then the test doesn't fail anymore. You could only detect failure by manually inspectingSystem.out.\$\endgroup\$CommentedDec 11, 2016 at 10:08
  • 1
    \$\begingroup\$yes, it's not a good approach.\$\endgroup\$CommentedDec 11, 2016 at 10:18
1
\$\begingroup\$

They are combining features of test runner with the test contraption. This is why the code has to "catch exception".

If they use a prebuilt test runner for table driven tests feature (as recommended above), I am sure they will not "catch exception".

answeredJun 3, 2022 at 17:43
Gerardo's user avatar
\$\endgroup\$

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.