Skip to content

Commit

Permalink
feat: use same container config for repository and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 15, 2023
1 parent 458daf0 commit 8463757
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
4 changes: 2 additions & 2 deletions inventory-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>21</java.version>
<spring-cloud.version>2023.0.0-M2</spring-cloud.version>
<spring-cloud.version>2023.0.0-RC1</spring-cloud.version>
<springdoc-openapi.version>2.2.0</springdoc-openapi.version>

<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
Expand Down Expand Up @@ -276,7 +276,7 @@
<configuration>
<database>
<type>POSTGRES</type>
<containerImage>postgres:16.0-alpine</containerImage>
<containerImage>postgres:16.1-alpine</containerImage>
<username>test</username>
<password>test</password>
<databaseName>test</databaseName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ Licensed under MIT License Copyright (c) 2021-2022 Raja Kolli.
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "inventory")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Inventory {
Expand All @@ -37,4 +35,24 @@ public class Inventory {

@Column(name = "reserved_items")
private Integer reservedItems = 0;

public Inventory setId(Long id) {
this.id = id;
return this;
}

public Inventory setProductCode(String productCode) {
this.productCode = productCode;
return this;
}

public Inventory setAvailableQuantity(Integer availableQuantity) {
this.availableQuantity = availableQuantity;
return this;
}

public Inventory setReservedItems(Integer reservedItems) {
this.reservedItems = reservedItems;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.

package com.example.inventoryservice;

import com.example.inventoryservice.common.ContainersConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
@Import(ContainersConfig.class)
public class TestInventoryApplication {

@Bean
@ServiceConnection
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("16.0-alpine"))
.withReuse(true);
}

@Bean
@ServiceConnection(name = "openzipkin/zipkin")
GenericContainer<?> zipkContainer() {
Expand All @@ -36,7 +31,7 @@ GenericContainer<?> zipkContainer() {
@Bean
@ServiceConnection
KafkaContainer kafkaContainer() {
return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.5.1"))
return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.5.2"))
.withKraft()
.withReuse(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/***
<p>
Licensed under MIT License Copyright (c) 2023 Raja Kolli.
</p>
***/

package com.example.inventoryservice.common;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {

@Bean
@ServiceConnection
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.1-alpine"))
.withReuse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.

import static org.assertj.core.api.Assertions.assertThat;

import com.example.inventoryservice.common.ContainersConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

@DataJpaTest(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16.0-alpine:///databasename"
"spring.test.database.replace=none"
})
@Import(ContainersConfig.class)
class InventoryRepositoryTest {

@Autowired private DataSource datasource;
Expand Down

0 comments on commit 8463757

Please sign in to comment.