Skip to content

Commit bc58d44

Browse files
committed
Rationalize DefaultRestartInitializerTests
Closes gh-14927
1 parent 44e9601 commit bc58d44

File tree

2 files changed

+32
-59
lines changed

2 files changed

+32
-59
lines changed

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/DefaultRestartInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected boolean isMain(Thread thread) {
7373
* @return {@code true} if the stack element means that the initializer should be
7474
* skipped
7575
*/
76-
protected boolean isSkippedStackElement(StackTraceElement element) {
76+
private boolean isSkippedStackElement(StackTraceElement element) {
7777
for (String skipped : SKIPPED_STACK_ELEMENTS) {
7878
if (element.getClassName().startsWith(skipped)) {
7979
return true;

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/DefaultRestartInitializerTests.java

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,87 +16,83 @@
1616

1717
package org.springframework.boot.devtools.restart;
1818

19-
import java.net.URL;
20-
2119
import org.junit.Test;
2220

2321
import static org.assertj.core.api.Assertions.assertThat;
24-
import static org.hamcrest.Matchers.nullValue;
22+
import static org.mockito.BDDMockito.given;
23+
import static org.mockito.Mockito.mock;
2524

2625
/**
2726
* Tests for {@link DefaultRestartInitializer}.
2827
*
2928
* @author Phillip Webb
3029
* @author Andy Wilkinson
30+
* @author Madhura Bhave
3131
*/
3232
public class DefaultRestartInitializerTests {
3333

3434
@Test
35-
public void nullForTests() {
36-
MockRestartInitializer initializer = new MockRestartInitializer(true);
37-
assertThat(initializer.getInitialUrls(Thread.currentThread())).isNull();
35+
public void jUnitStackShouldReturnNull() {
36+
testSkippedStacks("org.junit.runners.Something");
37+
}
38+
39+
@Test
40+
public void springTestStackShouldReturnNull() {
41+
testSkippedStacks("org.springframework.boot.test.Something");
42+
}
43+
44+
@Test
45+
public void cucumberStackShouldReturnNull() {
46+
testSkippedStacks("cucumber.runtime.Runtime.run");
3847
}
3948

4049
@Test
41-
public void validMainThread() {
42-
MockRestartInitializer initializer = new MockRestartInitializer(false);
50+
public void validMainThreadShouldReturnUrls() {
51+
DefaultRestartInitializer initializer = new DefaultRestartInitializer();
4352
ClassLoader classLoader = new MockAppClassLoader(getClass().getClassLoader());
4453
Thread thread = new Thread();
4554
thread.setName("main");
4655
thread.setContextClassLoader(classLoader);
47-
assertThat(initializer.isMain(thread)).isTrue();
48-
assertThat(initializer.getInitialUrls(thread)).isNotEqualTo(nullValue());
56+
assertThat(initializer.getInitialUrls(thread)).isNotNull();
4957
}
5058

5159
@Test
52-
public void threadNotNamedMain() {
53-
MockRestartInitializer initializer = new MockRestartInitializer(false);
60+
public void threadNotNamedMainShouldReturnNull() {
61+
DefaultRestartInitializer initializer = new DefaultRestartInitializer();
5462
ClassLoader classLoader = new MockAppClassLoader(getClass().getClassLoader());
5563
Thread thread = new Thread();
5664
thread.setName("buscuit");
5765
thread.setContextClassLoader(classLoader);
58-
assertThat(initializer.isMain(thread)).isFalse();
5966
assertThat(initializer.getInitialUrls(thread)).isNull();
6067
}
6168

6269
@Test
6370
public void threadNotUsingAppClassLoader() {
64-
MockRestartInitializer initializer = new MockRestartInitializer(false);
71+
DefaultRestartInitializer initializer = new DefaultRestartInitializer();
6572
ClassLoader classLoader = new MockLauncherClassLoader(
6673
getClass().getClassLoader());
6774
Thread thread = new Thread();
6875
thread.setName("main");
6976
thread.setContextClassLoader(classLoader);
70-
assertThat(initializer.isMain(thread)).isFalse();
7177
assertThat(initializer.getInitialUrls(thread)).isNull();
7278
}
7379

74-
@Test
75-
public void skipsDueToJUnitStacks() {
76-
testSkipStack("org.junit.runners.Something", true);
77-
}
78-
79-
@Test
80-
public void skipsDueToSpringTest() {
81-
testSkipStack("org.springframework.boot.test.Something", true);
82-
}
83-
84-
@Test
85-
public void skipsDueToCucumber() {
86-
testSkipStack("cucumber.runtime.Runtime.run", true);
87-
}
88-
8980
@Test
9081
public void urlsCanBeRetrieved() {
9182
assertThat(new DefaultRestartInitializer().getUrls(Thread.currentThread()))
9283
.isNotEmpty();
9384
}
9485

95-
private void testSkipStack(String className, boolean expected) {
96-
MockRestartInitializer initializer = new MockRestartInitializer(true);
97-
StackTraceElement element = new StackTraceElement(className, "someMethod",
98-
"someFile", 123);
99-
assertThat(initializer.isSkippedStackElement(element)).isEqualTo(expected);
86+
protected void testSkippedStacks(String s) {
87+
DefaultRestartInitializer initializer = new DefaultRestartInitializer();
88+
ClassLoader classLoader = new MockAppClassLoader(getClass().getClassLoader());
89+
Thread thread = mock(Thread.class);
90+
thread.setName("main");
91+
StackTraceElement element = new StackTraceElement(s, "someMethod", "someFile",
92+
123);
93+
given(thread.getStackTrace()).willReturn(new StackTraceElement[] { element });
94+
given(thread.getContextClassLoader()).willReturn(classLoader);
95+
assertThat(initializer.getInitialUrls(thread)).isEqualTo(null);
10096
}
10197

10298
private static class MockAppClassLoader extends ClassLoader {
@@ -115,27 +111,4 @@ private static class MockLauncherClassLoader extends ClassLoader {
115111

116112
}
117113

118-
private static class MockRestartInitializer extends DefaultRestartInitializer {
119-
120-
private final boolean considerStackElements;
121-
122-
MockRestartInitializer(boolean considerStackElements) {
123-
this.considerStackElements = considerStackElements;
124-
}
125-
126-
@Override
127-
protected boolean isSkippedStackElement(StackTraceElement element) {
128-
if (!this.considerStackElements) {
129-
return false;
130-
}
131-
return true;
132-
}
133-
134-
@Override
135-
protected URL[] getUrls(Thread thread) {
136-
return new URL[0];
137-
}
138-
139-
}
140-
141114
}

0 commit comments

Comments
 (0)