我正在測驗我的應用程式,但是在運行測驗用例時,我收到了 NUllPointer 例外,因為它無法映射 YML 檔案中的值。
你能告訴我如何實作這一目標嗎?
控制器類
class ControllerClass {
@Value("${app.items}")
String[] items; -- coming as null while running test cases
// remaing code
}
應用程式-test.yml
app:
items: a, b, c, d
測驗班
@SpringJUnitConfig
@SpringBootTest
@ActiveProfiles("test)
class TestControllerClass {
@InjectMock
ControllerClass controller;
@Mock
ServiceClass service;
@Test
//test case
}
uj5u.com熱心網友回復:
Mockito 不知道該怎么做 - 你可以手動完成:
@Before
public void setUp() {
String[] items = new String[2];
items[0] = "a";
items[1] = "b";
ReflectionTestUtils.setField(controller, "items",
items);
}
uj5u.com熱心網友回復:
自然,我們需要一個屬性檔案來定義我們想要用 @Value 注釋注入的值。因此,我們首先需要在我們的配置類中定義一個 @PropertySource — 帶有屬性檔案名。
@PropertySource("classpath:values.properties")
class ControllerClass {
@Value("${app.items}")
String[] items; -- coming as null while running test cases
// remaing code
}
如果它不作業使用。像這里提到的屬性檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341710.html
上一篇:比較來自2個檔案的資料并回傳不在任何一個檔案中的資料?
下一篇:獲取上個月的最后日期
