0

Here is the class that gets its pathToFile from application.properties.

@Component
public class CSVReader {

    @Value("${name.basics}")
    private String pathToFile;

   (other code)
}

The problem is: how can I substitute application.properties for testing?

  • https://www.baeldung.com/spring-tests-override-properties – Lesiak Feb 16 '21 at 21:20
  • 1
    Does this answer your question? [How do I mock an autowired @Value field in Spring with Mockito?](https://stackoverflow.com/questions/23162777/how-do-i-mock-an-autowired-value-field-in-spring-with-mockito) – Januson Feb 16 '21 at 21:22

1 Answers1

1

You can use spring boot test annotation:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"name.basics=whatever"})
public class YourTestClass {

@Autowired 
CSVReader cvsReader;

@Test
public void yourTest() {
//...

Second option is to put application-test.properties containing replacements in src/test/resources folder of your project

Dmitry
  • 87
  • 4