Skip to content

Commit

Permalink
Merge pull request #160 from nsteps/test-containres-feature
Browse files Browse the repository at this point in the history
Add test containers
  • Loading branch information
ekoutanov authored Aug 8, 2020
2 parents deaa4f6 + 5e249a4 commit bcd521d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<additionalOptions>-Xdoclint:none</additionalOptions>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<protobuf.version>3.11.1</protobuf.version>
<testcontainers.version>1.14.3</testcontainers.version>
</properties>

<scm>
Expand Down Expand Up @@ -177,6 +178,34 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- Spring test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/kafdrop/Kafdrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ public class Kafdrop {
private final static Logger LOG = LoggerFactory.getLogger(Kafdrop.class);

public static void main(String[] args) {
new SpringApplicationBuilder(Kafdrop.class)
.bannerMode(Mode.OFF)
.listeners(new EnvironmentSetupListener(),
new LoggingConfigurationListener())
.run(args);
createApplicationBuilder()
.run(args);
}


public static SpringApplicationBuilder createApplicationBuilder() {
return new SpringApplicationBuilder(Kafdrop.class)
.bannerMode(Mode.OFF)
.listeners(new EnvironmentSetupListener(),
new LoggingConfigurationListener());
}

@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> deploymentCustomizer() {
return factory -> {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/kafdrop/AbstractIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kafdrop;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.lifecycle.Startables;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class)
abstract class AbstractIntegrationTest {
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
static KafkaContainer kafka = new KafkaContainer();

public static Map<String, String> getProperties() {
Startables.deepStart(List.of(kafka)).join();
return Map.of("kafka.brokerConnect", kafka.getBootstrapServers());
}

@Override
@SuppressWarnings("unchecked, rawtypes")
public void initialize(ConfigurableApplicationContext context) {
var env = context.getEnvironment();
env.getPropertySources().addFirst(new MapPropertySource(
"testcontainers", (Map) getProperties()
));
}
}
}
8 changes: 8 additions & 0 deletions src/test/java/kafdrop/KafdropTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kafdrop;

import org.junit.Test;

public class KafdropTest extends AbstractIntegrationTest {
@Test
public void contextTest(){}
}
13 changes: 13 additions & 0 deletions src/test/java/kafdrop/LocalRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kafdrop;

/**
* Use this class for local development.
* It will run local kafka in docker with test containers.
*/
public class LocalRunner {
public static void main(String[] args) {
Kafdrop.createApplicationBuilder()
.initializers(new AbstractIntegrationTest.Initializer())
.run(args);
}
}

0 comments on commit bcd521d

Please sign in to comment.