1818
1919import java .lang .reflect .Method ;
2020
21- import org .junit .jupiter .api .BeforeEach ;
2221import org .junit .jupiter .api .Test ;
2322
2423import org .springframework .boot .loader .launch .FakeJarLauncher ;
@@ -37,13 +36,6 @@ class MainMethodTests {
3736
3837 private static final ThreadLocal <MainMethod > mainMethod = new ThreadLocal <>();
3938
40- private Method actualMain ;
41-
42- @ BeforeEach
43- void setup () throws Exception {
44- this .actualMain = Valid .class .getMethod ("main" , String [].class );
45- }
46-
4739 @ Test
4840 void threadMustNotBeNull () {
4941 assertThatIllegalArgumentException ().isThrownBy (() -> new MainMethod (null ))
@@ -52,9 +44,10 @@ void threadMustNotBeNull() {
5244
5345 @ Test
5446 void validMainMethod () throws Exception {
47+ Method actualMain = Valid .class .getMethod ("main" , String [].class );
5548 MainMethod method = new TestThread (Valid ::main ).test ();
56- assertThat (method .getMethod ()).isEqualTo (this . actualMain );
57- assertThat (method .getDeclaringClassName ()).isEqualTo (this . actualMain .getDeclaringClass ().getName ());
49+ assertThat (method .getMethod ()).isEqualTo (actualMain );
50+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
5851 }
5952
6053 @ Test // gh-35214
@@ -75,14 +68,41 @@ void viaJarLauncher() throws Exception {
7568 }
7669
7770 @ Test
78- void missingArgsMainMethod () {
79- assertThatIllegalStateException ().isThrownBy (() -> new TestThread (MissingArgs ::main ).test ())
80- .withMessageContaining ("Unable to find main method" );
71+ void detectPublicMainMethod () throws Exception {
72+ Method actualMain = PublicMainMethod .class .getMethod ("main" , String [].class );
73+ MainMethod method = new TestThread (PublicMainMethod ::main ).test ();
74+ assertThat (method .getMethod ()).isEqualTo (actualMain );
75+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
76+ }
77+
78+ @ Test
79+ void detectPublicParameterlessMainMethod () throws Exception {
80+ Method actualMain = PublicParameterlessMainMethod .class .getMethod ("main" );
81+ MainMethod method = new TestThread (PublicParameterlessMainMethod ::main ).test ();
82+ assertThat (method .getMethod ()).isEqualTo (actualMain );
83+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
84+ }
85+
86+ @ Test
87+ void detectPackagePrivateMainMethod () throws Exception {
88+ Method actualMain = PackagePrivateMainMethod .class .getDeclaredMethod ("main" , String [].class );
89+ MainMethod method = new TestThread (PackagePrivateMainMethod ::main ).test ();
90+ assertThat (method .getMethod ()).isEqualTo (actualMain );
91+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
92+ }
93+
94+ @ Test
95+ void detectPackagePrivateParameterlessMainMethod () throws Exception {
96+ Method actualMain = PackagePrivateParameterlessMainMethod .class .getDeclaredMethod ("main" );
97+ MainMethod method = new TestThread (PackagePrivateParameterlessMainMethod ::main ).test ();
98+ assertThat (method .getMethod ()).isEqualTo (actualMain );
99+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
81100 }
82101
83102 @ Test
84103 void nonStatic () {
85- assertThatIllegalStateException ().isThrownBy (() -> new TestThread (() -> new NonStaticMain ().main ()).test ())
104+ assertThatIllegalStateException ()
105+ .isThrownBy (() -> new TestThread (() -> new NonStaticMainMethod ().main ()).test ())
86106 .withMessageContaining ("Unable to find main method" );
87107 }
88108
@@ -141,15 +161,39 @@ public static void main(String... args) {
141161
142162 }
143163
144- public static class MissingArgs {
164+ public static class PublicMainMethod {
165+
166+ public static void main (String ... args ) {
167+ mainMethod .set (new MainMethod ());
168+ }
169+
170+ }
171+
172+ public static class PublicParameterlessMainMethod {
145173
146174 public static void main () {
147175 mainMethod .set (new MainMethod ());
148176 }
149177
150178 }
151179
152- public static class NonStaticMain {
180+ public static class PackagePrivateMainMethod {
181+
182+ static void main (String ... args ) {
183+ mainMethod .set (new MainMethod ());
184+ }
185+
186+ }
187+
188+ public static class PackagePrivateParameterlessMainMethod {
189+
190+ static void main () {
191+ mainMethod .set (new MainMethod ());
192+ }
193+
194+ }
195+
196+ public static class NonStaticMainMethod {
153197
154198 void main (String ... args ) {
155199 mainMethod .set (new MainMethod ());
0 commit comments