0

I have test class that consists of @Before method with some parameters, and other test methods.

@Before
@ParameterizedTest
@ValueSource(strings = {"123", "456"})
public void createData(String userId) {
someMethod(userId)
}


@ParameterizedTest
@ValueSource(strings = {"123", "456"})
public void abc(String userId) {}

but it keeps executing abc() first and throws error

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [java.lang.String arg0] in method [public void tests.xxx.createData(java.lang.String) throws java.io.IOException].

I'm quite new to JUnit Parameters. Can Before method be at the same time ParametrizedTest? Without this annotation [ParametrizedTests] it seems not to work as intended, but on the other hand I need this to execute before all other methods. What might be the issue and what's the solution?

jacob1989
  • 15
  • 3
  • Please clearify what do you really want to do? Run an ```init()``` method before each tests? In this case use ```@BeforeEach```. But keep in mind, it is not a test it's just a bootstrap. Otherwise if you need control the execution order of the test cases use ```@Order()``` annotation on test methods. – zforgo Sep 06 '21 at 13:32
  • I need this method to be run once before all other test methods (so in this case I presume I need to use `@BeforeAll` but then it has to be static or annotated with `@TestInstance(Lifecycle.PER_CLASS))`. But also, I need this `@BeforeAll` method (init method) to have parameters at entry point – jacob1989 Sep 06 '21 at 13:44
  • Parameterized tests are intended to create many different tests with the same logic (test method) but different values. Given that information, what should a before-all method do with the parameters since it is invoked only once? Which of the user IDs should be injected? – johanneslink Sep 06 '21 at 16:08
  • you're right. i just wanted to run it before the rest, so I think I should go with `@Order()`. But when I'm trying to do it with (1), (2) etc, it ships the first one (`createData`) and executes it at the end! – jacob1989 Sep 07 '21 at 07:00
  • OK i added `@TestMethodOrder(MethodOrderer.OrderAnnotation.class)` and it works – jacob1989 Sep 07 '21 at 08:19

0 Answers0