Search code examples
javajunit

How do I run the same test with multiple inputs?


I want to do a batch test like the following, however if there is an error, it does not tell me which value of i the error occurs in.

@Test
public void simpleMovingAverageBatch() {
    for (int i = 1; i < 61; ++i) {
        simpleMovingAverage(i);
    }
}

public void simpleMovingAverage(int days) {
    double sma = -1;
    int size = stock.getCloses().size();

    stock.calcSMA(days); // <- code that I'm testing

    // first days should be -1
    for (int index = size - 1; index >= size - days + 1; --index) {
        assertEquals(-1, stock.getSMA(days, index), 0.001);
    }

    for (int index = size - days - 1; index >= 0; --index) {
        sma = 0.0;
        for (int i = 0; i < days; ++i) {
            sma += stock.getCloses().get(index + i);
        }
        sma /= days;
        assertEquals(sma, stock.getSMA(days, index), 0.001);
    }
}

This is an example of what happens if I run the code as is:

Not what I want!

This is an example of what I want to have happen:

Something like this!

I've taken a look at Parameterized Tests in this tutorial, however I do not know if this the route I want to take.

Any tips?


I don't think this question is a duplicate of these:

Running the same JUnit test case multiple time with different data

Java unit test for different input data


Solution

  • The right way to do this test is to use a parameterized test as you describe in your question. However, you said you don't want to do that. So, instead, you can always use the String message argument for your failure message.

    You appear to be using assertEquals(double, expected, double actual, double delta) in this line:

    assertEquals(-1, stock.getSMA(days, index), 0.001);
    

    Instead, you could use assertEquals(String message, double expected, double actual, double delta) to give yourself a more informative error message, e.g.

    assertEquals(String.format("For index %d,", index), -1, stock.getSMA(days, index), 0.001);
    

    This will give you an error message like:

    java.lang.AssertionError: For index 15, expected:<-1.0> but was:<0.0>