Skip to content

Commit f91c8b6

Browse files
scordiosnicoll
authored andcommitted
Add missing null check in ApplicationContextAssertProvider
See gh-50456 Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
1 parent 3a37d16 commit f91c8b6

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public interface ApplicationContextAssertProvider<C extends ApplicationContext>
5555
extends ApplicationContext, AssertProvider<ApplicationContextAssert<C>>, Closeable {
5656

5757
/**
58-
* Return an assert for AspectJ.
59-
* @return an AspectJ assert
58+
* Return an assert for AssertJ.
59+
* @return an AssertJ assert
6060
* @deprecated to prevent accidental use. Prefer standard AssertJ
6161
* {@code assertThat(context)...} calls instead.
6262
*/
@@ -131,6 +131,7 @@ static <T extends ApplicationContextAssertProvider<C>, C extends ApplicationCont
131131
Assert.isTrue(type.isInterface(), "'type' must be an interface");
132132
Assert.notNull(contextType, "'contextType' must not be null");
133133
Assert.isTrue(contextType.isInterface(), "'contextType' must be an interface");
134+
Assert.notNull(contextSupplier, "'contextSupplier' must not be null");
134135
Class<?>[] interfaces = merge(new Class<?>[] { type, contextType }, additionalContextInterfaces);
135136
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces,
136137
new AssertProviderApplicationContextInvocationHandler(contextType, contextSupplier));

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
23-
import org.junit.jupiter.api.extension.ExtendWith;
24-
import org.mockito.Mock;
25-
import org.mockito.junit.jupiter.MockitoExtension;
2623

2724
import org.springframework.context.ApplicationContext;
2825
import org.springframework.context.ConfigurableApplicationContext;
@@ -32,18 +29,17 @@
3229
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3330
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3431
import static org.mockito.BDDMockito.then;
32+
import static org.mockito.Mockito.mock;
3533

3634
/**
3735
* Tests for {@link ApplicationContextAssertProvider} and
3836
* {@link AssertProviderApplicationContextInvocationHandler}.
3937
*
4038
* @author Phillip Webb
4139
*/
42-
@ExtendWith(MockitoExtension.class)
4340
class ApplicationContextAssertProviderTests {
4441

45-
@Mock
46-
private ConfigurableApplicationContext mockContext;
42+
private final ConfigurableApplicationContext mockContext = mock();
4743

4844
private RuntimeException startupFailure;
4945

@@ -69,35 +65,36 @@ void getWhenTypeIsNullShouldThrowException() {
6965

7066
@Test
7167
void getWhenTypeIsClassShouldThrowException() {
72-
assertThatIllegalArgumentException().isThrownBy(
73-
() -> ApplicationContextAssertProvider.get(null, ApplicationContext.class, this.mockContextSupplier))
74-
.withMessageContaining("'type' must not be null");
75-
}
76-
77-
@Test
78-
void getWhenContextTypeIsNullShouldThrowException() {
7968
assertThatIllegalArgumentException()
8069
.isThrownBy(() -> ApplicationContextAssertProvider.get(TestAssertProviderApplicationContextClass.class,
8170
ApplicationContext.class, this.mockContextSupplier))
8271
.withMessageContaining("'type' must be an interface");
8372
}
8473

8574
@Test
86-
void getWhenContextTypeIsClassShouldThrowException() {
75+
void getWhenContextTypeIsNullShouldThrowException() {
8776
assertThatIllegalArgumentException()
8877
.isThrownBy(() -> ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class, null,
8978
this.mockContextSupplier))
9079
.withMessageContaining("'contextType' must not be null");
9180
}
9281

9382
@Test
94-
void getWhenSupplierIsNullShouldThrowException() {
83+
void getWhenContextTypeIsClassShouldThrowException() {
9584
assertThatIllegalArgumentException()
9685
.isThrownBy(() -> ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class,
9786
StaticApplicationContext.class, this.mockContextSupplier))
9887
.withMessageContaining("'contextType' must be an interface");
9988
}
10089

90+
@Test
91+
void getWhenSupplierIsNullShouldThrowException() {
92+
assertThatIllegalArgumentException()
93+
.isThrownBy(() -> ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class,
94+
ApplicationContext.class, null))
95+
.withMessageContaining("'contextSupplier' must not be null");
96+
}
97+
10198
@Test
10299
void getWhenContextStartsShouldReturnProxyThatCallsRealMethods() {
103100
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);

0 commit comments

Comments
 (0)