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.
- \$\begingroup\$Side note, how are the digits in
984and7404increasing?\$\endgroup\$h.j.k.– h.j.k.2016-12-13 15:15:55 +00:00CommentedDec 13, 2016 at 15:15 - \$\begingroup\$I'm kinda forget, but here is the algorithmgithub.com/Gujarats/SolvingProblem/blob/master/src/main/…\$\endgroup\$Gujarat Santana– Gujarat Santana2016-12-14 06:19:38 +00:00CommentedDec 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\$h.j.k.– h.j.k.2016-12-14 15:44:20 +00:00CommentedDec 14, 2016 at 15:44
3 Answers3
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.
- 3\$\begingroup\$And here's JUnit5's dynamic tests thoughthe M3 implementation is still a bit limited\$\endgroup\$CAD97– CAD972016-12-11 17:05:11 +00:00CommentedDec 11, 2016 at 17:05
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.
- 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\$jonrsharpe– jonrsharpe2016-12-11 10:03:49 +00:00CommentedDec 11, 2016 at 10:03
- 1\$\begingroup\$But then the test doesn't fail anymore. You could only detect failure by manually inspecting
System.out.\$\endgroup\$Roland Illig– Roland Illig2016-12-11 10:08:01 +00:00CommentedDec 11, 2016 at 10:08 - 1\$\begingroup\$yes, it's not a good approach.\$\endgroup\$jonrsharpe– jonrsharpe2016-12-11 10:18:53 +00:00CommentedDec 11, 2016 at 10:18
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".
You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.
