Skip to content

Commit c98861b

Browse files
garyrussellartembilan
authored andcommitted
GH-1806: Fix Autowiring Broker in JUnit 5
Resolves #1806 When there is a test application context present, do not resolve the broker for parameter resolution. **cherry-pick to all supported branches**
1 parent cc0bdef commit c98861b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

spring-kafka-test/src/main/java/org/springframework/kafka/test/condition/EmbeddedKafkaCondition.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ public class EmbeddedKafkaCondition implements ExecutionCondition, AfterAllCallb
6565
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
6666
throws ParameterResolutionException {
6767

68-
return parameterContext.getParameter().getType().equals(EmbeddedKafkaBroker.class);
68+
if (BROKERS.get() == null) {
69+
return false;
70+
}
71+
else {
72+
return parameterContext.getParameter().getType().equals(EmbeddedKafkaBroker.class);
73+
}
6974
}
7075

7176
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.test.condition;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.kafka.test.EmbeddedKafkaBroker;
26+
import org.springframework.kafka.test.context.EmbeddedKafka;
27+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
28+
29+
/**
30+
* @author Gary Russell
31+
* @since 2.7.2
32+
*
33+
*/
34+
@SpringJUnitConfig
35+
@EmbeddedKafka
36+
public class WithSpringTestContextTests {
37+
38+
@Test
39+
void canAutowireBrokerInMethod(@Autowired EmbeddedKafkaBroker broker) {
40+
assertThat(broker).isNotNull();
41+
}
42+
43+
@Configuration
44+
static class Config {
45+
46+
}
47+
48+
}

0 commit comments

Comments
 (0)