Issue
I am testing my application but while running the test cases I am getting NUllPointer Exception as it's not able to map the value from YML file.
Can you please let me know how to achieve this ?
ControllerClass
class ControllerClass {
@Value("${app.items}")
String[] items; -- coming as null while running test cases
// remaing code
}
application-test.yml
app:
items: a, b, c, d
Test class
@SpringJUnitConfig
@SpringBootTest
@ActiveProfiles("test)
class TestControllerClass {
@InjectMock
ControllerClass controller;
@Mock
ServiceClass service;
@Test
//test case
}
Solution
Mockito doesn't know what to do - you can do it manually though:
@Before
public void setUp() {
String[] items = new String[2];
items[0] = "a";
items[1] = "b";
ReflectionTestUtils.setField(controller, "items",
items);
}
Answered By - Fosizzle
Answer Checked By - Terry (JavaFixing Volunteer)