-1

How to use JUnit 5 to run series of tests of a computation, each for a different pair of input data and benchmark output?

Condition: the computation should be performed only once per input and not for every test separately as it is time-intensive.

Every test checks a number put out by the already imperfectly written computation module. It is not feasible to restructure the module into separately testable subfunctions.

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
Mateusz
  • 39
  • 2
  • `DataModelTest.readModel(java.lang.String,java.lang.String)' could not be resolved` -- The class name is `ModelTest`, not `DataModelTest`. – Jim Garrison Jan 02 '18 at 20:55
  • 1
    See: https://stackoverflow.com/questions/46897134/how-to-implement-junit-4-parameterized-tests-in-junit-5 (@davidxxx's excellent answer) You appear to be misusing the @MethodSource annotation, i.e. "method must not accept any arguments". – geneSummons Jan 02 '18 at 22:36
  • @geneSummons - that solution is the standard in which the arguments are all read just by a test method. I would like to have a function that could read the arguments and feed the test methods with precomputed values - such as a constructor of the test class that would have them as an attribute. But this seems to be forbidden. – Mateusz Jan 03 '18 at 14:43

1 Answers1

0

The quick solution is to

  1. Read all data and perform all computations in the constructor, saving the outputs as an array/list and doing the same with benchmark outputs.

  2. Call @ParameterizedTests with @ValueSource taking an array of integers. They will iterate over the lists stored as object attributes using these integers as indices.

Mateusz
  • 39
  • 2