SpringJUnit4ClassRunner does not load the test properties file - java

I am trying to boot up a spring boot test, with a mapstruct implementation provided.
I am using the configuration below, but the application.properties file in the resources directory is not being read into the test.
How do I get the test below to read the default properties file, the file exist in test/resources and other spring boot tests are able to load it.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = SpringBootExampleConfig.class)
public class SpringBootExampleTest {
#Autowired
private IMapper mapper;
#Test
public void test() {
}
#Configuration
#ComponentScan(basePackageClasses = SpringBootExampleTest.class)
public static class SpringTestConfig {
}
}

Related

Why bean are not injected in test class

I have a jar which will be included in spring boot application, I am trying to do an integration test in this, the project has the configuration class for creating the data source and JDBC template, I am using testing,
There is no application class in this project when this jar included in another project that project fetches data perfectly fine but not in same project
spring-boot-starter-test is added as a dependency
Configuration
#Configuration
public class DatabaseAccesManagementConfig {
#Bean(name = "accessmngmtDataSource")
#Qualifier("accessmngmtDataSource")
#ConfigurationProperties(prefix = "accessmngmt.datasource")
public DataSource accessmngmtDataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name = "accessmngmtJdbcTemplate")
#Qualifier("accessmngmtJdbcTemplate")
public JdbcTemplate accessmngmtJdbcTemplate(#Qualifier("accessmngmtDataSource") DataSource accessmngmtDataSource) {
return new JdbcTemplate(accessmngmtDataSource);
}
}
Dao class
#Repository
public class ResourcePrivilegesDao {
static final Logger log = LoggerFactory.getLogger(ResourcePrivilegesDao.class);
#Autowired
#Qualifier("accessmngmtJdbcTemplate")
private JdbcTemplate jdbcTemplate;
public List<RP> getAll() {
log.debug("entering getAll()");
String sql = "SELECT * FROM rp";
RowMapper<RP> rowMapper = new RPRowMapper();
List<RP> result = this.jdbcTemplate.query(sql, rowMapper);
return result;
}
}
Test class
#SpringBootTest
#TestPropertySource(locations="classpath:application-test.properties")
#ContextConfiguration(classes = DatabaseAccesManagementConfig.class)
public class ResourcePrivilegesDaoTest {
#Autowired
DatabaseAccesManagementConfig databaseAccesManagement;
#Autowired
ResourcePrivilegesDao dao;
#Test
public void testGetAll() {
System.out.println(databaseAccesManagement);
List<ResourcePrivileges> list = dao.getAll();
Assert.notNull(list, "No resource privileges found");
Assert.notEmpty(list);
}
}
test property inside
environment=test
#Access management db details
accessmngmt.database.url=//xxyyy/am
accessmngmt.database.username=user
accessmngmt.database.password=password
In the test class, you missed the #RunWith(SpringRunner.class) which configure a unit test that need Spring's DI.
Take a look for the doc spring unit test
In order for the unit test to run a batch job, the framework must load the job's ApplicationContext. Two annotations are used to trigger this:
#RunWith(SpringJUnit4ClassRunner.class): Indicates that the class should use Spring's JUnit facilities
#ContextConfiguration(locations = {...}): Indicates which XML files contain the ApplicationContext.
Notice that, the SpringRunner is an alias for the SpringJUnit4ClassRunner. So we can use #RunWith(SpringRunner.class) instead of #RunWith(SpringJUnit4ClassRunner.class) with a shorter name.
Updated:
For the datasource properties injection, The #EnableConfigurationProperties annotation should annotated on the Test class.
Also, you use accessmngmt.datasource in DatabaseAccesManagementConfig class, while the prefix is not matched accessmngmt.database in application-test.properties. Here you must unify them, so you can inject the properties into the bean.

Spring Boot test with yaml properties by profile

So there's a lot of hits on this topic, but none of them have worked for me.
I have a very simple configuration class:
#Configuration
#ConfigurationProperties(prefix = "props")
public class TagIncluder {
private static final String PARAMETER_NAME = "tags";
private List<String> tags;
public TagIncluder() {
tags = new ArrayList<>();
}
public List<String> getTags() {
return tags;
}
#Handler
public void attachIncludedTags(Exchange exchange) {
exchange.getIn().setHeader(PARAMETER_NAME, tags);
}
}
I want this class to be able to load different property files. I am using yaml, and my file is named application-tag_test.yml. I have tried placing this file in src/main/resources, src/test/resources and src/test/resources/config, but it is never picked up.
This is the contents of the property file:
props:
tags:
- test
And finally, the test case:
#SpringBootTest
#ActiveProfiles("tag_test")
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = TagIncluder.class)
public class TagIncluderTest extends ExchangeTestSupport {
#Autowired
private TagIncluder sut;
#Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
Additionally, I have tried placing an application.properties file in the above described locations with the following content:
spring.profiles.active=tag_test
What is required for Spring to set my yaml file as the desired configuration for my test class under test?
UPDATE
So after some exploration and trial and error, I have found that the following works:
#SpringBootTest
#ActiveProfiles("tag_test")
#RunWith(SpringJUnit4ClassRunner.class)
public class TagIncluderTest extends ExchangeTestSupport {
#Autowired
private TagIncluder sut;
#Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
The difference here is that I've removed the #ContextConfiguration annotation and I let Spring take care of all of that.
It is a lot slower, and I would prefer specifying what is needed. I think this might break in the future, for instance if I add another configuration class that will start with the entire context and throw errors because those properties are not included in my application-tag_test.yml configuration.
Finally, any of the above locations I tried for the configuration is valid with the above annotations. The application.properties to specify a profile is not needed.
If anyone knows a way to specify what should be loaded into the context instead, I'd be very grateful for another solution.
With some guidance of Jans suggestion above, I've managed to isolate the test to a slice. Auto configured testing is written about here, however that only touches on Springs predefined #..Test annotations.
If you dive deeper into the #WebMvcTest, for instance, you will find the #ImportAutoConfiguration annotation.
Using this, we can tell our test class to enable auto configuration for a single slice of our application. A tutorial is available here. The full list of factories available for auto configuration can be found in the spring-boot repository.
Finally, this is the entire test class:
#ActiveProfiles("tag_test")
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = TagIncluder.class)
#ImportAutoConfiguration(classes = ConfigurationPropertiesAutoConfiguration.class)
public class TagIncluderTest extends ExchangeTestSupport {
#Autowired
private TagIncluder sut;
#Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
The class under test is untouched.
So now we can:
Use profiles
Use yaml
Test only our desired class in Spring Context
This has been very enlightening :)
The Spring Boot Test documentations states that
External properties, logging, and other features of Spring Boot are installed in the context by default only if you use SpringApplication to create it.
This means that you need to have a working Spring Boot Application in order to test anything related to property loading in a test case.
Also, setting a list from properties needs a setter. This works:
#Configuration
#ConfigurationProperties(prefix = "props")
public class TagIncluder {
private List<String> tags;
public void setTags(List<String> tags) {
this.tags = tags;
}
public List<String> getTags() {
return tags;
}
}
#Component
public class MyComponent {
#Autowired
TagIncluder tagIncluder;
}
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
#SpringBootTest
#ActiveProfiles("tag_test")
#RunWith(SpringRunner.class)
public class TagIncluderTest {
#Autowired
private TagIncluder sut;
#Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
System.out.println(sut.getTags());
}
}

dynamically load test property file and service property file in spring boot

Writing junit for spring boot service class. My problem is, I am having two properties files(one for application and another for test) During junit i want to load test property file and during application, i want to load application properties file.But always it loads my application service properties file.
Service.java
#Component
#PropertySource("classpath:service.properties")
public class webModelService implements IWebModelService<webModel> {
#Value("${service.common.software.url}")
private String softwareEndPoint;
#Value("${service.common.software.url}")
private String createwebEndpoint;
#Value("${service.common.software.delete.url}")
private String deletewebEndpoint;
#Value("${service.common.thing.url}")
private String createthingEndPoint;
#Override
public void save(WebModel wModel) {
log.info("Save web model -> start");
System.out.println("softwareEndPoint===>"+softwareEndPoint);
System.out.println("createwebEndpoint===>"+createwebEndpoint);
System.out.println("deletewebEndpoint===>"+deletewebEndpoint);
System.out.println("createthingEndPoint===>"+createthingEndPoint);
}
}
Junit.java
#RunWith(SpringJUnit4ClassRunner.class)
#ComponentScan("com.ericsson.tmo.iotep.dataimport")
#TestPropertySource("classpath:Test-service.properties")
#ContextConfiguration(classes = { BeansForDefaultValueGenerator.class }, loader = AnnotationConfigContextLoader.class)
public class webModelServiceTest {
#Autowired
webModelService webService;
#Test
public void testwebModelService(){
nwebModel.setNotes("Test_notes");
List<Software> softwareList = new ArrayList<>();
software.setSoftwareName("Test_software");
softwareList.add(software);
anwebModel.setSoftware(softwareList);
webService.save(anwebModel);
}
}
service.properties
service.common.software.url=http://192.168.99.100:8080/softwares
service.common.thing.url=http://192.168.99.100:8080/thing
service.common.software.url=http://192.168.99.100:8080/deviceModels
service.common.software.delete.url=http://192.168.99.100:8080/deviceModels/
Test-service.properties
service.common.software.url=http://localhost:8083/softwares
service.common.thing.url=http://localhost:8083/thing
service.common.software.url=http://localhost:8083/deviceModels
service.common.software.delete.url=http://localhost:8083/deviceModels/
And I need to load test-service.properties file during junit and i need to load service.properties during my applicartion run
your test properties file should be located in test folder (in resources)
if your properties file named by application.properties (application-{profile}.properties) and properties file for testing application-test.properties, spring boot load properties hierarchy will be : booting application.properties and then load application-test.properties file, spring overrides values in application properties from application-test properties.
(Spring properties)
if you want to tell spring where it should search properties filed for testing you could use something like that:
#TestPropertySource({"classpath:/application.properties",classpath:/application-test.properties"})
#ActiveProfiles(profiles = "test")
Use the PropertySourcesPlaceholderConfigurer in order to mention the property for service and override with test configuration when you run the test through Junit
Something like this
`
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
#Configuration
public class PropertyTestConfiguration {
#Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(ArrayUtils.addAll(
new PathMatchingResourcePatternResolver().getResources("classpath*:application.properties"),
new PathMatchingResourcePatternResolver().getResources("classpath*:test.properties")
)
);
return ppc;
}
}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class PropertyTests {
#Value("${elastic.index}")
String index;
#Configuration
#Import({PropertyTestConfiguration.class})
static class ContextConfiguration {
}
}
`

junit spring boot profile properties not being read

I am trring to read a value from the properties file in my junit setup using spring boot.
I can not read the value. Below is my content:-
application-test.properties
my.user.name=Amar
COnfig file to create beans:
#Configuration
#ActiveProfiles("test")
#Profile("test")
public class RdbmsTestConfig {
#Value("${my.user.name}")
private String name;
#Bean
public String myString(){
return "Amar";
}
#Bean
public PropsHolder propsHolder() {
PropsHolder propsHolder = new PropsHolder();
propsHolder.setUserName(name);
return propsHolder;
}
}
My test class:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = RdbmsTestConfig.class)
#ActiveProfiles("test")
public class TestRoomService {
#Autowired
#Qualifier("myString")
private String myString;
#Autowired
private PropsHolder propsHolder;
#Autowired
private Environment env;
#Test
public void userTest() {
Arrays.stream(env.getActiveProfiles()).forEach(System.out::println);
System.out.println(propsHolder.getUserName());
Assert.assertNotNull(myString);
Assert.assertEquals("Amar",myString);
}
}
The value for propsHolder.getUserName comes out to be ${my.user.name}
First remove #ActiveProfiles("test") from your class RdbmsTestConfig. Then your test just defines the RdbmsTestConfig as spring context. As I can see you do not run a real spring boot test. The problem is you do not have any PropertyPlaceholderConfigurer configured in your spring config. So either configure one PropertyPlaceholderConfigurer or add #SpringBootTest to your test if you have any SpringBootApplication.
I've never used #Profile(), so i'm not sure if that is supposed to do what you want it to do. But I'm always using #PropertySources(), because otherwise, how is the code supposed to know where to look for the properties?
#Configuration
#PropertySources(value = { #PropertySource("classpath:core-
test.properties") })
I created a base test class that has the required annotations:-
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = RdbmsTestConfig.class)
#ActiveProfiles("test")
#SpringBootTest
public abstract class BastTest { }
#ActiveProfiles set the profile to use used, I dont have to mention it in the application.properties file
My test class now extends this:-
public class TestRoomService extends BastTest
In my RdbmsTestConfig remove #ActiveProfiles annotation.

#ComponentScan not working in test with spring-boot-starter-test

I am attempting to test my #Service and #Repository classes in my project with spring-boot-starter-test and #Autowired is not working for the classes I'm testing.
Unit test:
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration(classes = HelloWorldConfiguration.class
//#SpringApplicationConfiguration(classes = HelloWorldRs.class)
//#ComponentScan(basePackages = {"com.me.sbworkshop", "com.me.sbworkshop.service"})
//#ConfigurationProperties("helloworld")
//#EnableAutoConfiguration
//#ActiveProfiles("test")
// THIS CLASS IS IN src/test/java/ AND BUILDS INTO target/test-classes
public class HelloWorldTest {
#Autowired
HelloWorldMessageService helloWorldMessageService;
public static final String EXPECTED = "je pense donc je suis-TESTING123";
#Test
public void testGetMessage() {
String result = helloWorldMessageService.getMessage();
Assert.assertEquals(EXPECTED, result);
}
}
Service:
#Service
#ConfigurationProperties("helloworld")
// THIS CLASS IS IN /src/main/java AND BUILDS INTO target/classes
public class HelloWorldMessageService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message=message;
}
}
The commented class annotations on the unit test represent the various things I've tried to get this working. The test and the project packages are in the same package paths and the #ComponentScan works fine from my entry point (#RestController class with main method). The service #ComponentScan's and #Autowire's fine in my #RestController class in the src/main/java side, but does not in the test. I am required to add it again as a #Bean in my #Configuration class in order for #Autowired to work. The class is otherwise in scope just fine and I can reference and instantiate it just fine from the test. The problem appears to be that #ComponentScan does not appear to correctly traverse multiple entries in my test runner classpath, in this case /target/test-classes and /target/classes.
The IDE I am using is IntelliJ IDEA 13.
UPDATE - here are HelloWorldRs and its config:
#RestController
#EnableAutoConfiguration
#ComponentScan
public class HelloWorldRs {
// SPRING BOOT ENTRY POINT - main() method
public static void main(String[] args) {
SpringApplication.run(HelloWorldRs.class);
}
#Autowired
HelloWorldMessageService helloWorldMessageService;
#RequestMapping("/helloWorld")
public String helloWorld() {
return helloWorldMessageService.getMessage();
}
}
...
#Configuration
public class HelloWorldConfiguration {
#Bean
public Map<String, String> map() {
return new HashMap<>();
}
// This bean was manually added as a workaround to the #ComponentScan problem
#Bean
public HelloWorldMessageService helloWorldMessageService() {
return new HelloWorldMessageService();
}
// This bean was manually added as a workaround to the #ComponentScan problem
#Bean
public HelloWorldRs helloWorldRs() {
return new HelloWorldRs();
}
}
First, I'd recommend to use a newer #RunWith(SpringRunner.class) but that makes no difference, it is just shorter (and recommended).
Second, from the #EnableAutoConfiguration I see that you are using spring boot - which is certainly a good thing. There are some good reasons why not to use #ComponentScan directly. Can you try the following?
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration(classes=YourApplication_or_other_Configuration.class)
public class HelloWorldTest {
... etc.
I don't know if this will turn out to be the solution, but don't use the default package (i.e. don't put *.java in "src/main/java" directly), and definitely don't use a #ComponentScan or #EnableAutoConfiguration in the default package. You will end up killing your application on startup as it tries to scan everything on the classpath (including all the Spring libraries).
SpringBoot 2.7.3, JUnit 5.8.2
If you want to have full control about the spring's configuration (and not rely on the hidden magic of auto configuration) I suggest to create an explicit configuration class:
#ComponentScan(basePackages = { "my.package.to.scan" })
public class MySpringTestConfig
{
// just for spring configuration annotations
}
and reference it in your test class:
#ContextConfiguration(classes = { MySpringTestConfig.class })
#ExtendWith({ SpringExtension.class })
class MySpringTest
{
...
}

Categories