Reputation: 1237
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
Upvotes: 2
Views: 10008
Reputation: 2881
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.
Upvotes: 5