Testing / JUnit6 Interview Questions
What is @ParameterizedClass in JUnit 6 and how does it differ from @ParameterizedTest?
@ParameterizedClass (introduced in JUnit 5.13 and fully supported in JUnit 6) parameterises an entire test class rather than a single method. All test methods in the class run for each set of arguments. This is ideal when multiple tests share the same setup conditions that vary per invocation.
import org.junit.jupiter.params.ParameterizedClass; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.api.*; // @ParameterizedClass: the ENTIRE class runs once per argument @ParameterizedClass @MethodSource("locales") @DisplayName("Cart service in locale") class CartServiceLocaleTest { // Arguments injected via constructor or field private final Locale locale; CartServiceLocaleTest(Locale locale) { this.locale = locale; } static Stream<Locale> locales() { return Stream.of(Locale.US, Locale.UK, Locale.DE); } @Test void currencySymbolIsCorrect() { CartService svc = new CartService(locale); assertNotNull(svc.getCurrencySymbol()); } @Test void priceFormattingMatchesLocale() { CartService svc = new CartService(locale); String price = svc.format(9.99); assertTrue(price.contains(svc.getCurrencySymbol())); } } // Runs 2 tests x 3 locales = 6 test invocations total // Compare with @ParameterizedTest (method-level): class CartServiceTest { @ParameterizedTest @MethodSource("locales") void currencySymbolIsCorrect(Locale locale) { // single method only assertNotNull(new CartService(locale).getCurrencySymbol()); } }
| Aspect | @ParameterizedTest | @ParameterizedClass |
|---|---|---|
| Scope | Single test method | Entire test class (all @Test methods) |
| Arguments injected via | Method parameter | Constructor or field injection |
| Use case | One test scenario with multiple inputs | Multiple tests sharing the same parameterised setup |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
