Reputation: 7601
How can I load test properties from a file like 'application-test.properties'?
The file is stored in the src/test/resources folder. I put the file also in all possible folders as well. When running the test as part of the Maven test run, all works fine. When running the new (single) test from the (IntelliJ) IDE, each time I get same the error message:
Caused by: java.io.FileNotFoundException: class path resource [application-test.properties] cannot be opened because it does not exist
This is the test class:
@RunWith(SpringRunner.class)
@EnableAutoConfiguration
@ComponentScan(basePackages = {"nl.deholtmans.tjm1706.todolist"})
@PropertySource( "application-test.properties")
public class TodoListServiceTest {
@Autowired
TodoListService todoListService;
@Test
public void testBasic() { ... }
It looks that I have to run the test first time from Maven. Why is that?
Upvotes: 0
Views: 180
Reputation: 125272
Spring Boot will automatically load the correct properties file if the profile is activated. In a test you can use the @ActiveProfiles
annotation for that.
Next you would need to make sure that you actually use the proper Spring Boot infrastructure to run your test, using @SpringBootTest
. That being said your test header should look something like the following
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class TodoListServiceTest { ... }
And ofcourse make sure that your IDE builds the application before running the tests.
Upvotes: 1