@@ -174,10 +174,38 @@ void providesArgumentsUsingListOfObjectArrays() {
174174 }
175175
176176 @ Test
177- void throwsExceptionWhenNonStaticFactoryMethodIsReferencedAndStaticIsRequired () {
177+ void throwsExceptionWhenNonStaticLocalFactoryMethodIsReferencedWithLifecyclePerMethodSemantics () {
178+ var lifecyclePerClass = false ;
178179 var exception = assertThrows (PreconditionViolationException .class ,
179- () -> provideArguments (NonStaticTestCase .class , null , false , "nonStaticStringStreamProvider" ).toArray ());
180+ () -> provideArguments (NonStaticTestCase .class , lifecyclePerClass ,
181+ "nonStaticStringStreamProvider" ).toArray ());
180182
183+ assertStaticIsRequired (exception );
184+ }
185+
186+ @ Test
187+ void throwsExceptionWhenNonStaticExternalFactoryMethodIsReferencedWithLifecyclePerMethodSemantics () {
188+ var factoryClass = NonStaticTestCase .class .getName ();
189+ var factoryMethod = factoryClass + "#nonStaticStringStreamProvider" ;
190+ var lifecyclePerClass = false ;
191+ var exception = assertThrows (PreconditionViolationException .class ,
192+ () -> provideArguments (TestCase .class , lifecyclePerClass , factoryMethod ).toArray ());
193+
194+ assertStaticIsRequired (exception );
195+ }
196+
197+ @ Test
198+ void throwsExceptionWhenNonStaticExternalFactoryMethodIsReferencedWithLifecyclePerClassSemantics () {
199+ var factoryClass = NonStaticTestCase .class .getName ();
200+ var factoryMethod = factoryClass + "#nonStaticStringStreamProvider" ;
201+ boolean lifecyclePerClass = true ;
202+ var exception = assertThrows (PreconditionViolationException .class ,
203+ () -> provideArguments (TestCase .class , lifecyclePerClass , factoryMethod ).toArray ());
204+
205+ assertStaticIsRequired (exception );
206+ }
207+
208+ private static void assertStaticIsRequired (PreconditionViolationException exception ) {
181209 assertThat (exception ).hasMessageContainingAll ("Method '" ,
182210 "' must be static: local factory methods must be static " ,
183211 "unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS); " ,
@@ -186,7 +214,7 @@ void throwsExceptionWhenNonStaticFactoryMethodIsReferencedAndStaticIsRequired()
186214
187215 @ Test
188216 void providesArgumentsFromNonStaticFactoryMethodWhenStaticIsNotRequired () {
189- var arguments = provideArguments (NonStaticTestCase .class , null , true , "nonStaticStringStreamProvider" );
217+ var arguments = provideArguments (NonStaticTestCase .class , true , "nonStaticStringStreamProvider" );
190218
191219 assertThat (arguments ).containsExactly (array ("foo" ), array ("bar" ));
192220 }
@@ -609,7 +637,7 @@ void throwsExceptionWhenExternalFactoryMethodHasInvalidReturnType() {
609637 String factoryMethod = factoryClass + "#factoryWithInvalidReturnType" ;
610638
611639 var exception = assertThrows (PreconditionViolationException .class ,
612- () -> provideArguments (TestCase .class , null , false , factoryMethod ).toArray ());
640+ () -> provideArguments (TestCase .class , false , factoryMethod ).toArray ());
613641
614642 assertThat (exception .getMessage ())//
615643 .containsSubsequence ("Could not find valid factory method [" + factoryMethod + "] for test class [" ,
@@ -676,30 +704,24 @@ private static Object[] array(Object... objects) {
676704 }
677705
678706 private Stream <Object []> provideArguments (String ... factoryMethodNames ) {
679- return provideArguments (TestCase .class , null , false , factoryMethodNames );
707+ return provideArguments (TestCase .class , false , factoryMethodNames );
680708 }
681709
682710 private Stream <Object []> provideArguments (Method testMethod , String factoryMethodName ) {
683711 return provideArguments (TestCase .class , testMethod , false , factoryMethodName );
684712 }
685713
686- private Stream <Object []> provideArguments (Class <?> testClass , Method testMethod , boolean allowNonStaticMethod ,
714+ private Stream <Object []> provideArguments (Class <?> testClass , boolean allowNonStaticMethod ,
687715 String ... factoryMethodNames ) {
688716
689- if (testMethod == null ) {
690- try {
691- class DummyClass {
692- @ SuppressWarnings ("unused" )
693- public void dummyMethod () {
694- };
695- }
696- // ensure we have a non-null method, even if it's not a real test method.
697- testMethod = DummyClass .class .getMethod ("dummyMethod" );
698- }
699- catch (Exception ex ) {
700- throw new RuntimeException (ex );
701- }
702- }
717+ // Ensure we have a non-null test method, even if it's not a real test method.
718+ // If this throws an exception, make sure that the supplied test class defines a "void test()" method.
719+ Method testMethod = ReflectionUtils .findMethod (testClass , "test" ).get ();
720+ return provideArguments (testClass , testMethod , allowNonStaticMethod , factoryMethodNames );
721+ }
722+
723+ private Stream <Object []> provideArguments (Class <?> testClass , Method testMethod , boolean allowNonStaticMethod ,
724+ String ... factoryMethodNames ) {
703725
704726 var methodSource = mock (MethodSource .class );
705727
@@ -940,6 +962,9 @@ static Object[][] twoDimensionalObjectArrayProvider() {
940962 // This test case mimics @TestInstance(Lifecycle.PER_CLASS)
941963 static class NonStaticTestCase {
942964
965+ void test () {
966+ }
967+
943968 Stream <String > nonStaticStringStreamProvider () {
944969 return Stream .of ("foo" , "bar" );
945970 }
0 commit comments