1

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,

enter image description here

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.

enter image description here

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. enter image description here

Any thoughts?

Arefe
  • 11,321
  • 18
  • 114
  • 168

1 Answers1

1

As per the Spring documentation, the @ContextConfiguration lookup the resources. So, you could create a resource folder, preferably under /test folder, with the structure com/puut/bitcoin/dao/test/config/datasource.xml. This way the spring configuration should be able to read the resources for your test. So, your folder structure should look like:

/test/java - contains your test cases
/test/resources - contains your XML/configuration files
Uday
  • 1,165
  • 9
  • 12
  • Do you mean I Should put all inside the 'test/resources/' ? – Arefe Jun 09 '17 at 04:53
  • I mean under `/test`. Like `test/java` contains your test cases and `test/resources` contains your XML files/resources. – Uday Jun 09 '17 at 04:56