I have this spring boot application in which I have the below line inside a method.
BufferedReader reader = new BufferedReader(new FileReader("somePath"));
How can I inject this into my code so I can mock it for my unit tests? Using guice I could use a provider. But how can I achieve this using spring boot? Any help would be much appreciated.
If you want to mock the class which have the below line inside a method.
BufferedReader reader = new BufferedReader(new FileReader("somePath"));
Create a Mock instance of the class and define the mock behaviour like :
@MockBean
private TestClass testClass;
when(testClass.readFile()).thenReturn(content);
where content is the output you want to return.
you can create a bean of buffered reader and inject :
@Bean
BufferedReader reader(@Value("${filename}") String fileName) throws FileNotFoundException{
return new BufferedReader(new FileReader(fileName));
}
-Dfilename=SOMEPATH