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,9 +68,19 @@ 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 missingArgsMainMethod () throws Exception {
72+ Method actualMain = MissingArgs .class .getMethod ("main" );
73+ MainMethod method = new TestThread (MissingArgs ::main ).test ();
74+ assertThat (method .getMethod ()).isEqualTo (actualMain );
75+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
76+ }
77+
78+ @ Test
79+ void missingArgsPackagePrivateMainMethod () throws Exception {
80+ Method actualMain = MissingArgsPackagePrivate .class .getDeclaredMethod ("main" );
81+ MainMethod method = new TestThread (MissingArgsPackagePrivate ::main ).test ();
82+ assertThat (method .getMethod ()).isEqualTo (actualMain );
83+ assertThat (method .getDeclaringClassName ()).isEqualTo (actualMain .getDeclaringClass ().getName ());
8184 }
8285
8386 @ Test
@@ -149,6 +152,14 @@ public static void main() {
149152
150153 }
151154
155+ public static class MissingArgsPackagePrivate {
156+
157+ static void main () {
158+ mainMethod .set (new MainMethod ());
159+ }
160+
161+ }
162+
152163 public static class NonStaticMain {
153164
154165 void main (String ... args ) {
0 commit comments