Skip to content

Commit bb95a0c

Browse files
committed
Integration test runs with testcontainers.
1 parent eaaa3e0 commit bb95a0c

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

pom.xml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<groupId>com.trendyol</groupId>
1212
<artifactId>recom-engine-web-service</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
14-
<name>recom-engine</name>
14+
<name>recom-engine-web-service</name>
1515
<description>Demo project for Spring Boot</description>
1616

1717
<properties>
@@ -43,8 +43,21 @@
4343
<artifactId>spring-boot-starter-data-mongodb</artifactId>
4444
</dependency>
4545
<dependency>
46-
<groupId>de.flapdoodle.embed</groupId>
47-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
46+
<groupId>org.testcontainers</groupId>
47+
<artifactId>testcontainers</artifactId>
48+
<version>1.11.3</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-api</artifactId>
54+
<version>RELEASE</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.junit.jupiter</groupId>
59+
<artifactId>junit-jupiter-api</artifactId>
60+
<version>RELEASE</version>
4861
<scope>test</scope>
4962
</dependency>
5063
</dependencies>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.trendyol.recomengine.webservice.controller;
2+
3+
import com.trendyol.recomengine.webservice.model.Recommendation;
4+
import com.trendyol.recomengine.webservice.repository.RecommendationRepository;
5+
import com.trendyol.recomengine.webservice.service.RecommendationService;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.util.TestPropertyValues;
12+
import org.springframework.context.ApplicationContextInitializer;
13+
import org.springframework.context.ConfigurableApplicationContext;
14+
import org.springframework.context.event.ContextClosedEvent;
15+
import org.springframework.test.context.ActiveProfiles;
16+
import org.springframework.test.context.ContextConfiguration;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
import org.testcontainers.containers.GenericContainer;
19+
import org.testcontainers.containers.wait.strategy.Wait;
20+
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
23+
@ActiveProfiles("test")
24+
@ContextConfiguration(initializers = BaseMongoIntegrationTest.Initializer.class)
25+
public class BaseMongoIntegrationTest {
26+
private static final int MONGO_PORT = 27017;
27+
28+
private static GenericContainer mongoContainer = new GenericContainer("mongo:latest")
29+
.withExposedPorts(MONGO_PORT)
30+
.waitingFor(Wait.forListeningPort());
31+
@Autowired
32+
RecommendationRepository recommendationRepository;
33+
@Autowired
34+
RecommendationService recommendationService;
35+
36+
@Test
37+
public void dummyTest() {
38+
String userId = "3";
39+
String[] recommendations = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
40+
41+
Recommendation recommendation = new Recommendation(userId, recommendations);
42+
recommendationRepository.save(recommendation);
43+
Assert.assertEquals(recommendationService.getRecommendations(userId).get_id(), userId);
44+
}
45+
46+
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
47+
@Override
48+
public void initialize(ConfigurableApplicationContext applicationContext) {
49+
mongoContainer.start();
50+
51+
TestPropertyValues.of("spring.data.mongodb.uri=mongodb://localhost:" + mongoContainer.getMappedPort(MONGO_PORT))
52+
.applyTo(applicationContext);
53+
54+
applicationContext.addApplicationListener(
55+
applicationEvent -> {
56+
if (applicationEvent instanceof ContextClosedEvent) {
57+
mongoContainer.stop();
58+
}
59+
}
60+
);
61+
}
62+
}
63+
}
64+

src/test/java/com/trendyol/recomengine/webservice/controller/RecommendationControllerIntegrationTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
import org.junit.runner.RunWith;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.ActiveProfiles;
1011
import org.springframework.test.context.ContextConfiguration;
1112
import org.springframework.test.context.junit4.SpringRunner;
1213
import org.springframework.test.context.support.AnnotationConfigContextLoader;
13-
14-
import static org.assertj.core.api.Assertions.assertThat;
14+
import org.testcontainers.containers.GenericContainer;
1515

1616
@RunWith(SpringRunner.class)
17-
@SpringBootTest
18-
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
17+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
18+
@ActiveProfiles("test")
1919
public class RecommendationControllerIntegrationTests {
2020
@Autowired
2121
RecommendationRepository recommendationRepository;
@@ -31,10 +31,12 @@ public void contextLoad() {
3131
@Test
3232
public void givenUserObject_whenSave_thenCreateNewUser() {
3333
String userId = "3";
34-
String recommendations[] = new String[] {"1","2","3","4","5","6","7","8","9","10"};
34+
String[] recommendations = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
3535

3636
Recommendation recommendation = new Recommendation(userId, recommendations);
3737
recommendationRepository.save(recommendation);
38-
assert(recommendationService.getRecommendations(userId).get_id().equals(userId));
38+
assert (recommendationService.getRecommendations(userId).get_id().equals(userId));
3939
}
40+
4041
}
42+

0 commit comments

Comments
 (0)