1919import java .lang .reflect .Method ;
2020
2121import org .jspecify .annotations .Nullable ;
22- import org .junit .jupiter .api .BeforeEach ;
2322import org .junit .jupiter .api .Test ;
2423
2524import org .springframework .boot .loader .launch .FakeJarLauncher ;
@@ -38,13 +37,6 @@ class MainMethodTests {
3837
3938 private static final ThreadLocal <MainMethod > mainMethod = new ThreadLocal <>();
4039
41- private Method actualMain ;
42-
43- @ BeforeEach
44- void setup () throws Exception {
45- this .actualMain = Valid .class .getMethod ("main" , String [].class );
46- }
47-
4840 @ Test
4941 @ SuppressWarnings ("NullAway" ) // Test null check
5042 void threadMustNotBeNull () {
@@ -54,9 +46,10 @@ void threadMustNotBeNull() {
5446
5547 @ Test
5648 void validMainMethod () throws Exception {
49+ Method actualMain = Valid .class .getMethod ("main" , String [].class );
5750 MainMethod method = new TestThread (Valid ::main ).test ();
58- assertThat (method .getMethod ()).isEqualTo (this . actualMain );
59- assertThat (method .getDeclaringClassName ()).isEqualTo (this . actualMain .getDeclaringClass ().getName ());
51+ assertThat (method .getMethod ()).isEqualTo (actualMain );
52+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
6053 }
6154
6255 @ Test // gh-35214
@@ -77,14 +70,41 @@ void viaJarLauncher() throws Exception {
7770 }
7871
7972 @ Test
80- void missingArgsMainMethod () {
81- assertThatIllegalStateException ().isThrownBy (() -> new TestThread (MissingArgs ::main ).test ())
82- .withMessageContaining ("Unable to find main method" );
73+ void detectPublicMainMethod () throws Exception {
74+ Method actualMain = PublicMainMethod .class .getMethod ("main" , String [].class );
75+ MainMethod method = new TestThread (PublicMainMethod ::main ).test ();
76+ assertThat (method .getMethod ()).isEqualTo (actualMain );
77+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
78+ }
79+
80+ @ Test
81+ void detectPublicParameterlessMainMethod () throws Exception {
82+ Method actualMain = PublicParameterlessMainMethod .class .getMethod ("main" );
83+ MainMethod method = new TestThread (PublicParameterlessMainMethod ::main ).test ();
84+ assertThat (method .getMethod ()).isEqualTo (actualMain );
85+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
86+ }
87+
88+ @ Test
89+ void detectPackagePrivateMainMethod () throws Exception {
90+ Method actualMain = PackagePrivateMainMethod .class .getDeclaredMethod ("main" , String [].class );
91+ MainMethod method = new TestThread (PackagePrivateMainMethod ::main ).test ();
92+ assertThat (method .getMethod ()).isEqualTo (actualMain );
93+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
94+ }
95+
96+ @ Test
97+ void detectPackagePrivateParameterlessMainMethod () throws Exception {
98+ Method actualMain = PackagePrivateParameterlessMainMethod .class .getDeclaredMethod ("main" );
99+ MainMethod method = new TestThread (PackagePrivateParameterlessMainMethod ::main ).test ();
100+ assertThat (method .getMethod ()).isEqualTo (actualMain );
101+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
83102 }
84103
85104 @ Test
86105 void nonStatic () {
87- assertThatIllegalStateException ().isThrownBy (() -> new TestThread (() -> new NonStaticMain ().main ()).test ())
106+ assertThatIllegalStateException ()
107+ .isThrownBy (() -> new TestThread (() -> new NonStaticMainMethod ().main ()).test ())
88108 .withMessageContaining ("Unable to find main method" );
89109 }
90110
@@ -145,15 +165,39 @@ public static void main(String... args) {
145165
146166 }
147167
148- public static class MissingArgs {
168+ public static class PublicMainMethod {
169+
170+ public static void main (String ... args ) {
171+ mainMethod .set (new MainMethod ());
172+ }
173+
174+ }
175+
176+ public static class PublicParameterlessMainMethod {
149177
150178 public static void main () {
151179 mainMethod .set (new MainMethod ());
152180 }
153181
154182 }
155183
156- public static class NonStaticMain {
184+ public static class PackagePrivateMainMethod {
185+
186+ static void main (String ... args ) {
187+ mainMethod .set (new MainMethod ());
188+ }
189+
190+ }
191+
192+ public static class PackagePrivateParameterlessMainMethod {
193+
194+ static void main () {
195+ mainMethod .set (new MainMethod ());
196+ }
197+
198+ }
199+
200+ public static class NonStaticMainMethod {
157201
158202 void main (String ... args ) {
159203 mainMethod .set (new MainMethod ());
0 commit comments