'Overriding Configuration properties with test container in Micronaut not working
Test container configuration
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public abstract class TestContainerFixture {
@Container
protected static final GenericContainer mongoDBContainer;
protected static final Map<String, Object> properties;
static {
mongoDBContainer = new GenericContainer(DockerImageName.parse("mongo:4.0.10"))
.withExposedPorts(27017)
.withReuse(true);
mongoDBContainer.start();
properties = Map.of("mongodb.uri",
String.format("mongodb://%s:%s/FeteBird-Product",
mongoDBContainer.getContainerIpAddress(),
mongoDBContainer.getMappedPort(27017)));
}
protected static ApplicationContext applicationContext;
}
Application.yml
mongodb:
uri: "mongodb://${MONGO_HOST:localhost}:${MONGO_PORT:27017}/FeteBird-Product"
database: "FeteBird-Product"
JUnit Testing
@MicronautTest(rebuildContext = true)
public class SubCategoryCreateTest extends TestContainerFixture {
@BeforeAll
public static void initUnitTest() {
applicationContext = ApplicationContext.run(properties, ConstantValues.TEST_ENVIRONMENT);
}
@Test
@DisplayName("Should create sub category with valid claim as Owner")
void shouldCreateSubCategoryWithValidClaimAsOwner() {
// Given
HttpResponse<CategoryModel> categoryrsp = this.httpRequestToCreateCategory();
assertEquals(categoryrsp.getStatus(), HttpStatus.CREATED);
// When
SubCategoryModel subCategoryModel = new SubCategoryModel(null, "Men swim sweat", "Men swim seat description");
HttpRequest request = HttpRequest.POST(String.format("%s/%s/%s", "category", categoryrsp.body().id(), "/sub-category"), subCategoryModel)
.bearerAuth(bearerAccessRefreshToken.getAccessToken());
HttpResponse<SubCategoryModel> rsp = client.toBlocking().exchange(request, SubCategoryModel.class);
var item = rsp.body();
// Then
assertEquals(rsp.getStatus(), HttpStatus.CREATED);
assertTrue(item.id() != null);
}
}
The configuration is not over-ridden in the service layer, Controller has the dependency on service layer
Update after implementing TestPropertyProvider works
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class TestContainerFixture implements TestPropertyProvider {
protected GenericContainer mongoDBContainer;
@Override
public Map<String, String> getProperties() {
mongoDBContainer = new GenericContainer(DockerImageName.parse("mongo:4.0.10"))
.withExposedPorts(27017)
.withReuse(true);
mongoDBContainer.start();
return Map.of("mongodb.uri",
String.format("mongodb://%s:%s/FeteBird-Product",
mongoDBContainer.getContainerIpAddress(),
mongoDBContainer.getMappedPort(27017)));
}
}
@MicronautTest
public class SubCategoryCreateTest extends TestPropertyProvider {
@Test
@DisplayName("Should create sub category with valid claim as Owner")
void shouldCreateSubCategoryWithValidClaimAsOwner() {
// Given
HttpResponse<CategoryModel> categoryrsp = this.httpRequestToCreateCategory();
assertEquals(categoryrsp.getStatus(), HttpStatus.CREATED);
// When
SubCategoryModel subCategoryModel = new SubCategoryModel(null, "Men swim sweat", "Men swim seat description");
HttpRequest request = HttpRequest.POST(String.format("%s/%s/%s", "category", categoryrsp.body().id(), "/sub-category"), subCategoryModel)
.bearerAuth(bearerAccessRefreshToken.getAccessToken());
HttpResponse<SubCategoryModel> rsp = client.toBlocking().exchange(request, SubCategoryModel.class);
var item = rsp.body();
// Then
assertEquals(rsp.getStatus(), HttpStatus.CREATED);
assertTrue(item.id() != null);
}
}
Solution 1:[1]
You need to implement TestPropertyProperty and set up your database URL in the getProperties method that it requires
See the guide here for an example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tim_yates |

