I am trying to test a method, but when my test method calls the actual method, due to @Value field present , the actual method always receives the value defined under @Value field i.e. null. You can have a look at the code for actual method and the test method below:
Actual method
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);
}
}
Below is my test method:
@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");
}
Below is CreateIndexRequest class
method:
public CreateIndexRequest(String index) {
if (index == null) {
throw new IllegalArgumentException("The index name cannot be null.");
} else {
this.index = index;
}
}
What happening is, when my test method calls the actual method indexService.index("65","{\"name\":molly}","1");
, then the control goes to the actual method
, and the private method createIndex
is injecting index
value which is defined above as @Value("${elasticsearch.index}") private String index;
. and hence in CreateIndexRequest method
, it always evaluates to null and throws exception IllegalArgumentException("The index name cannot be null.")
.
I tried using ReflectionTestUtils.setField
but the required dependency of org.springframework.test.util.ReflectionTestUtils
is not there in my project. Is there any other way to mock @Value field?