【发布时间】:2021-06-30 19:43:09
【问题描述】:
我正在尝试测试一个方法,但是当我的测试方法调用实际方法时,由于存在@Value 字段,实际方法总是接收@Value 字段下定义的值,即 null。您可以看看下面的实际方法和测试方法的代码:
实际方法
public class IndexService {
@Value("${elasticsearch.index}")
private String index;
public boolean index(String id, String json, String index) {
try {
createIndex();
return true;
} catch (IOException e) {
log.warn("Exception in indexing data {}", e.getMessage());
}
return false;
}
private void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest(index);
}
}
下面是我的测试方法:
@Test
public void IndexServiceIndex() throws IOException {
CreateIndexRequest request1 = new CreateIndexRequest(index);
request1.source("{\"name\":true}",XContentType.JSON);
Mockito.when(indicesClient.create(request1,RequestOptions.DEFAULT))
.thenReturn(createIndexResponse);
Boolean indexser = indexService.index("65","{\"name\":molly}","1");
}
下面是CreateIndexRequest class方法:
public CreateIndexRequest(String index) {
if (index == null) {
throw new IllegalArgumentException("The index name cannot be null.");
} else {
this.index = index;
}
}
发生的情况是,当我的测试方法调用实际方法indexService.index("65","{\"name\":molly}","1");,然后控制转到actual method,私有方法createIndex 正在注入index 值,上面定义为 @Value("${elasticsearch.index}") private String index; .因此在 CreateIndexRequest method 中,它总是评估为 null 并抛出异常 IllegalArgumentException("The index name cannot be null.")。
我尝试使用ReflectionTestUtils.setField,但我的项目中没有org.springframework.test.util.ReflectionTestUtils 所需的依赖项。还有其他方法可以模拟@Value 字段吗?
【问题讨论】:
-
查看具有
properties参数的@SpringBootTest -
你能给我一些链接来阅读如何使用它吗?还有没有其他方法可以仅使用 mockito 来解决问题?
-
答案是不要。更改您的类以使用构造函数注入,然后只需调用
new并将值作为普通参数传递给它。 -
谢谢@chrylis-cautiouslyoptimistic-,我会试试的。
标签: java spring-boot unit-testing junit mockito