Skip to content

Commit

Permalink
Adding test cases for placeholder resolution, both with and without t…
Browse files Browse the repository at this point in the history
…he placeholder set
  • Loading branch information
helios2k6 committed May 6, 2024
1 parent f72e850 commit e23f14c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface Engine {
int getCylinders();

String start();

String getDescription();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

// tag::imports[]
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.Nullable;

import jakarta.inject.Singleton;
// end::imports[]
Expand All @@ -28,6 +29,10 @@ public class EngineImpl implements Engine {
@Value("${my.engine.cylinders:6}") // <1>
protected int cylinders;

@Nullable
@Value("${my.engine.description}")
protected String description;

@Override
public int getCylinders() {
return cylinders;
Expand All @@ -38,5 +43,10 @@ public String start() {// <2>
return "Starting V" + getCylinders() + " Engine";
}

@Override
public String getDescription() {
return description;
}

}
// end::class[]
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class VehicleSpec {

Expand Down Expand Up @@ -56,4 +57,36 @@ void testStartVehicleWithoutConfiguration() {
assertEquals("Starting V6 Engine", vehicle.start());
}

@Test
void testStartVehicleWithNonEmptyPlaceholder() {
// tag::start[]
ApplicationContext applicationContext = new DefaultApplicationContext("test");
LinkedHashMap<String, Object> map = new LinkedHashMap(1);
map.put("my.engine.description", "${DESCRIPTION}");
map.put("DESCRIPTION", "V8 Engine");
applicationContext.getEnvironment().addPropertySource(PropertySource.of("test", map));
applicationContext.start();

Vehicle vehicle = applicationContext.getBean(Vehicle.class);
DefaultGroovyMethods.println(this, vehicle.start());
// end::start[]

assertEquals("V8 Engine", vehicle.getEngine().getDescription());
}

@Test
void testStartVehicleWithEmptyPlaceholder() {
// tag::start[]
ApplicationContext applicationContext = new DefaultApplicationContext("test");
LinkedHashMap<String, Object> map = new LinkedHashMap(1);
map.put("my.engine.description", "${DESCRIPTION}");
applicationContext.getEnvironment().addPropertySource(PropertySource.of("test", map));
applicationContext.start();

Vehicle vehicle = applicationContext.getBean(Vehicle.class);
DefaultGroovyMethods.println(this, vehicle.start());
// end::start[]

assertNull(vehicle.getEngine().getDescription());
}
}

0 comments on commit e23f14c

Please sign in to comment.