{"record":{"id":"ff0470155cc8b298","repo":"junit-team/junit5","slug":"failed-to-find-constructor-for-s-s-please-ens","errorCode":null,"errorMessage":"Failed to find constructor for %s [%s]. Please ensure that a no-argument or a single constructor exists.","messagePattern":"Failed to find constructor for (.+?) \\[(.+?)\\]\\. Please ensure that a no-argument or a single constructor exists\\.","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestSpiInstantiator.java","lineNumber":72,"sourceCode":"\n\t\tConstructor<?>[] constructors = implementationClass.getDeclaredConstructors();\n\n\t\t// Single constructor?\n\t\tif (constructors.length == 1) {\n\t\t\treturn constructors[0];\n\t\t}\n\t\t// Find default constructor.\n\t\tfor (Constructor<?> constructor : constructors) {\n\t\t\tif (constructor.getParameterCount() == 0) {\n\t\t\t\treturn constructor;\n\t\t\t}\n\t\t}\n\t\t// Otherwise...\n\t\tString message = \"\"\"\n\t\t\t\tFailed to find constructor for %s [%s]. \\\n\t\t\t\tPlease ensure that a no-argument or a single constructor exists.\"\"\".formatted(\n\t\t\tspiInterface.getSimpleName(), implementationClass.getName());\n\t\tthrow new JUnitException(message);\n\t}\n\n\tprivate ParameterizedTestSpiInstantiator() {\n\t}\n\n}\n","sourceCodeStart":54,"sourceCodeEnd":79,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestSpiInstantiator.java#L54-L79","documentation":"Thrown by ParameterizedTestSpiInstantiator.findBestConstructor() when attempting to instantiate an SPI implementation class (such as a custom ArgumentsProvider, ArgumentConverter, ArgumentsAggregator, or ArgumentsSource) that has multiple declared constructors and none of them is a no-argument constructor. The instantiator first checks for a single constructor (returns it regardless of arity), then looks for a no-arg constructor among multiple, and if neither exists, throws this. The %s placeholders are filled with the SPI interface simple name and the implementation class fully-qualified name.","triggerScenarios":"Implement ArgumentsProvider (or any parameterized test SPI) as a class with multiple constructors where none has zero parameters. For example, a custom ArgumentsProvider with both a constructor taking a String and one taking an int, but no default constructor. The instantiator is called when JUnit processes @ArgumentsSource(MyProvider.class) or similar annotations.","commonSituations":"Custom ArgumentsProvider or ArgumentConverter classes that evolved to have multiple constructors for different initialization modes. Inner classes mistakenly declared as non-static inner classes (which implicitly get a constructor with the outer-class parameter — but this is caught earlier by the isInnerClass check). Classes that use dependency injection frameworks and have constructors with injected parameters.","solutions":["Add a public no-argument constructor to the SPI implementation class","Reduce to a single constructor (any arity) — the instantiator will use it if it's the only one","If the class needs configuration, use annotation-based configuration (implement AnnotationConsumer) rather than constructor parameters","Ensure the class is a top-level class or a static nested class, not a non-static inner class"],"exampleFix":"// before — multiple constructors, no no-arg\npublic class MyArgumentsProvider implements ArgumentsProvider {\n    public MyArgumentsProvider(String config) { ... }\n    public MyArgumentsProvider(int count) { ... }\n}\n\n// after — add a no-arg constructor\npublic class MyArgumentsProvider implements ArgumentsProvider {\n    public MyArgumentsProvider() { }\n    public MyArgumentsProvider(String config) { ... }\n}\n\n// OR — single constructor (no-arg)\npublic class MyArgumentsProvider implements ArgumentsProvider {\n    public MyArgumentsProvider() { }\n}","handlingStrategy":"validation","validationCode":"// Validate that an SPI implementation class has a usable constructor before passing it to JUnit\nimport java.lang.reflect.Constructor;\n\nstatic <T> boolean hasValidSpiConstructor(Class<? extends T> implClass) {\n    Constructor<?>[] ctors = implClass.getDeclaredConstructors();\n    if (ctors.length == 1) return true;\n    for (Constructor<?> c : ctors) {\n        if (c.getParameterCount() == 0) return true;\n    }\n    return false;\n}\n\n// Usage:\nif (!hasValidSpiConstructor(MyArgumentsProvider.class)) {\n    throw new IllegalStateException(\n        MyArgumentsProvider.class.getName() + \" must have a no-arg or single constructor\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always provide a public no-argument constructor on custom SPI implementations (ArgumentsProvider, ArgumentConverter, ArgumentsAggregator)","Use static nested classes or top-level classes for SPI implementations — non-static inner classes have a hidden outer-class parameter","Prefer annotation-based configuration (AnnotationConsumer) over constructor parameters for configuring SPI implementations"],"tags":["spi","instantiation","constructor","reflection","junit-params"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}