Skip to content

Commit 0107e22

Browse files
Merge pull request #87 from AnswerConsulting/BENCH-187-TEST-CONTAINERS
BENCH-187 TEST CONTAINERS
2 parents 1a667f6 + 82a212f commit 0107e22

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@
9292
<version>1.33</version>
9393
<scope>compile</scope>
9494
</dependency>
95+
96+
<dependency>
97+
<groupId>org.testcontainers</groupId>
98+
<artifactId>testcontainers</artifactId>
99+
<version>1.17.5</version>
100+
<scope>test</scope>
101+
</dependency>
102+
103+
<dependency>
104+
<groupId>org.testcontainers</groupId>
105+
<artifactId>junit-jupiter</artifactId>
106+
<version>1.17.5</version>
107+
<scope>test</scope>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>org.testcontainers</groupId>
112+
<artifactId>mysql</artifactId>
113+
<version>1.17.5</version>
114+
<scope>test</scope>
115+
</dependency>
95116
</dependencies>
96117

97118
<build>

src/main/resources/application.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ spring:
4545
username: test_user
4646
password: GS3ef_fsd^!
4747
url: jdbc:mysql://localhost:3306/answer_king_test
48+
name: answer_king_test
49+
sql:
50+
init:
51+
mode: never
4852

4953
---
5054
spring:

src/main/resources/init_db.sql

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
SET NAMES utf8mb4;
3+
SET FOREIGN_KEY_CHECKS = 0;
4+
5+
-- ----------------------------
6+
-- Table structure for category
7+
-- ----------------------------
8+
DROP TABLE IF EXISTS `category`;
9+
CREATE TABLE `category` (
10+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
11+
`name` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
12+
`description` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
13+
`retired` bit(1) NOT NULL,
14+
PRIMARY KEY (`id`) USING BTREE
15+
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
16+
17+
-- ----------------------------
18+
-- Records of category
19+
-- ----------------------------
20+
21+
-- ----------------------------
22+
-- Table structure for order
23+
-- ----------------------------
24+
DROP TABLE IF EXISTS `order`;
25+
CREATE TABLE `order` (
26+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
27+
`address` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
28+
`order_status` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
29+
PRIMARY KEY (`id`) USING BTREE
30+
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
31+
32+
-- ----------------------------
33+
-- Records of order
34+
-- ----------------------------
35+
INSERT INTO `order` VALUES (1, 'Leeds', 'IN_PROGRESS');
36+
INSERT INTO `order` VALUES (2, 'Manchester', 'IN_PROGRESS');
37+
38+
-- ----------------------------
39+
-- Table structure for product
40+
-- ----------------------------
41+
DROP TABLE IF EXISTS `product`;
42+
CREATE TABLE `product` (
43+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
44+
`name` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
45+
`description` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
46+
`retired` bit(1) NOT NULL,
47+
`price` decimal(12, 2) NOT NULL,
48+
PRIMARY KEY (`id`) USING BTREE
49+
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
50+
51+
-- ----------------------------
52+
-- Records of product
53+
-- ----------------------------
54+
INSERT INTO `product` VALUES (1, 'Burger', '300g Beef', b'0', 6.69);
55+
INSERT INTO `product` VALUES (2, 'Cola', '500ml', b'1', 2.99);
56+
INSERT INTO `product` VALUES (3, 'Fries', 'Large Fries', b'0', 2.99);
57+
58+
-- ----------------------------
59+
-- Table structure for order_product
60+
-- ----------------------------
61+
DROP TABLE IF EXISTS `order_product`;
62+
CREATE TABLE `order_product` (
63+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
64+
`order_id` bigint(20) NOT NULL,
65+
`product_id` bigint(20) NOT NULL,
66+
`quantity` int(11) NOT NULL,
67+
PRIMARY KEY (`id`) USING BTREE,
68+
INDEX `order_id`(`order_id`) USING BTREE,
69+
INDEX `product_id`(`product_id`) USING BTREE,
70+
CONSTRAINT `order_product_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `order` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
71+
CONSTRAINT `order_product_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
72+
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
73+
74+
-- ----------------------------
75+
-- Records of order_product
76+
-- ----------------------------
77+
INSERT INTO `order_product` VALUES (1, 1, 1, 5);
78+
INSERT INTO `order_product` VALUES (2, 1, 3, 47);
79+
INSERT INTO `order_product` VALUES (3, 2, 1, 1);
80+
INSERT INTO `order_product` VALUES (4, 2, 3, 1);
81+
82+
-- ----------------------------
83+
-- Table structure for product_category
84+
-- ----------------------------
85+
DROP TABLE IF EXISTS `product_category`;
86+
CREATE TABLE `product_category` (
87+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
88+
`product_id` bigint(20) NOT NULL,
89+
`category_id` bigint(20) NOT NULL,
90+
PRIMARY KEY (`id`) USING BTREE,
91+
INDEX `product_id`(`product_id`) USING BTREE,
92+
INDEX `category_id`(`category_id`) USING BTREE,
93+
CONSTRAINT `product_category_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
94+
CONSTRAINT `product_category_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
95+
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
96+
97+
-- ----------------------------
98+
-- Records of product_category
99+
-- ----------------------------
100+
101+
SET FOREIGN_KEY_CHECKS = 1;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.answerdigital.answerking;
2+
3+
import com.answerdigital.answerking.utility.AbstractContainerBaseTest;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.mock.web.MockHttpServletResponse;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.RequestBuilder;
13+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
17+
18+
@SpringBootTest
19+
@AutoConfigureMockMvc(addFilters = false)
20+
class AnswerKingApplicationTest extends AbstractContainerBaseTest
21+
{
22+
@Autowired
23+
private MockMvc mockMvc;
24+
25+
@Test
26+
void getAllProductsReturnListOfProductObjects() throws Exception {
27+
//when
28+
RequestBuilder request = MockMvcRequestBuilders.get("/products");
29+
MockHttpServletResponse response = mockMvc.perform(request).andReturn().getResponse();
30+
31+
//then
32+
assertEquals(HttpStatus.OK.value(), response.getStatus());
33+
assertFalse(response.getContentAsString().isEmpty());
34+
ObjectMapper mapper = new ObjectMapper();
35+
assertEquals("Burger", mapper.readTree(response.getContentAsString()).get(0).get("name").textValue());
36+
assertEquals("300g Beef", mapper.readTree(response.getContentAsString()).get(0).get("description").textValue());
37+
assertEquals(6.69, mapper.readTree(response.getContentAsString()).get(0).get("price").asDouble());
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.answerdigital.answerking.utility;
2+
3+
import org.springframework.test.context.ActiveProfiles;
4+
import org.springframework.test.context.DynamicPropertyRegistry;
5+
import org.springframework.test.context.DynamicPropertySource;
6+
import org.testcontainers.containers.MySQLContainer;
7+
8+
@ActiveProfiles("test")
9+
public abstract class AbstractContainerBaseTest {
10+
private static final MySQLContainer MYSQL_CONTAINER;
11+
12+
static {
13+
MYSQL_CONTAINER =
14+
new MySQLContainer<>("mysql:8.0.31")
15+
.withInitScript("init_db.sql")
16+
.withReuse(true);
17+
MYSQL_CONTAINER.start();
18+
}
19+
20+
@DynamicPropertySource
21+
private static void overrideProps(DynamicPropertyRegistry registry) {
22+
registry.add("spring.datasource.url", MYSQL_CONTAINER::getJdbcUrl);
23+
registry.add("spring.datasource.username", MYSQL_CONTAINER::getUsername);
24+
registry.add("spring.datasource.password", MYSQL_CONTAINER::getPassword);
25+
registry.add("spring.datasource.name", MYSQL_CONTAINER::getDatabaseName);
26+
}
27+
}

0 commit comments

Comments
 (0)