I'm attempting to perform a junit test on my controller which receives a variable from application.yml file and prints it out.
Properties class in src/main/java:
@Data
@Validated
@ConfigurationProperties(EchoProperties.PREFIX)
@Component
public class EchoProperties {
public static final String PREFIX = "vmh0.echo";
private String message;
}
Controller class that autowires the properties class also in src/main/java:
@RestController
@RequestMapping("/api/echo-echoplusplus")
public class EchoPlusPlusController {
@Autowired
EchoProperties echoProperties;
@GetMapping("/echomessage")
public EchoPlusPlusResponse echoMessage() {
return new EchoPlusPlusResponse(new EchoPlusPlusRes(echoProperties.getMessage()));
}
}
I have an application.yml in my src/main/resources and application-test.yml in my src/test/resources:
vmh0:
echo:
message: "hello world"
Test class in src/test/java:
@EnableConfigurationProperties
@ContextConfiguration(classes = EchoProperties.class)
class EchoPlusPlusControllerTest {
@InjectMocks
EchoPlusPlusController echoPlusPlusController;
@Autowired
EchoProperties echoProperties;
private MockMvc mockMvc;
@BeforeEach()
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(echoPlusPlusController).build();
assertNotNull(mockMvc);
assertNotNull(echoPlusPlusController);
}
@Test
final void testEchoMessageSuccess() throws Exception {
try {
MockHttpServletResponse mockGetMessageResponse = mockMvc
.perform(MockMvcRequestBuilders.get("/api/echo-echoplusplus/echomessage")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse();
String responseString = mockGetMessageResponse.getContentAsString();
EchoPlusPlusResponse actualResponse = new ObjectMapper().readValue(responseString,
EchoPlusPlusResponse.class);
System.out.println("greeting: " + echoProperties.getMessage());
assertNotNull(mockGetMessageResponse);
assertEquals("hello world!", actualResponse.getEchoPlusPlusRes().getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Error:
java.lang.NullPointerException: Cannot invoke "com.config.properties.EchoProperties.getMessage()" because "this.echoProperties" is null
at com.controller.EchoPlusPlusControllerTest.testReadProperty(EchoPlusPlusControllerTest.java:42)
The project is functionally correct when it runs, but can't figure out how to have my EchoProperties class read from application-test.yml during testing.
I'm using JUnit 5 and Springboot 3