I work on Spring web MVC project and try to write some test for the database operations. The test class is provided below,
@ActiveProfiles("dev")
@ContextConfiguration(locations = {
"classpath:com/puut/bitcoin/config/dao-context.xml",
// "classpath:com/puut/bitcoin/config/security-context.xml",
"classpath:com/puut/bitcoin/dao/test/config/datasource.xml"
})
@RunWith(SpringJUnit4ClassRunner.class)
public class OfferDaoTests {
@Autowired
private DataSource dataSource;
@Autowired
private OffersDao offersDao;
@Before
public void init() {
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.execute("delete from offers");
jdbc.execute("delete from users");
jdbc.execute("delete from authorities");
}
@Test
public void testCreateUser() {
Offer offer = new Offer("johnwpurcell", "john@caveofprogramming.com", "This is a test offer.");
assertTrue("Offer creation should return true", offersDao.create(offer));
List<Offer> offers = offersDao.getOffers();
assertEquals("Should be one offer in database.", 1, offers.size());
assertEquals("Retrieved offer should match created offer.", offer, offers.get(0));
// Get the offer with ID filled in.
offer = offers.get(0);
offer.setText("Updated offer text.");
assertTrue("Offer update should return true", offersDao.update(offer));
Offer updated = offersDao.getOffer(offer.getId());
assertEquals("Updated offer should match retrieved updated offer", offer, updated);
offersDao.delete(offer.getId());
List<Offer> empty = offersDao.getOffers();
assertEquals("Offers lists should be empty.", 0, empty.size());
}
}
The project structure is provided below,
I get the error informing that the datasource.xml
can't be opened, because, it doesn't exist. Its indeed exist and the path is correct as well.
Caused by: java.io.FileNotFoundException: class path resource [com/puut/bitcoin/dao/test/config/datasource.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
In the datasource.xml
file, I see that the Application context is not configured for this file
and IntelliJ
is literally provided some option to do so.
This is not very clear to me as I have defined this file in the OfferDaoTests
as following,
@ContextConfiguration(locations = {
"classpath:com/puut/bitcoin/config/dao-context.xml",
// "classpath:com/puut/bitcoin/config/security-context.xml",
"classpath:com/puut/bitcoin/dao/test/config/datasource.xml"
})
I don't see the point to define the file in the web.xml
as well. Because, this is only to test the database operations. Also, suddenly gets some error in the web.xml
which was not here a while ago.
Any thoughts?