java.io.FileNotFoundException:无法打开类路径资源

时间:2017-06-09 01:07:36

标签: java xml spring spring-mvc

我在Spring Web MVC项目上工作并尝试为数据库操作编写一些测试。测试类如下所示,

@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());
    }
}

项目结构如下:

enter image description here

我收到错误通知无法打开datasource.xml,因为它不存在。它确实存在,路径也是正确的。

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)

datasource.xml文件中,我看到Application context is not configured for this fileIntelliJ字面上提供了一些选项。

enter image description here

这对我来说不是很清楚,因为我在OfferDaoTests中定义了这个文件如下,

@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"
    })

我也没有看到在web.xml中定义文件的重点。因为,这只是为了测试数据库操作。此外,突然在web.xml中出现了一些错误,这在以前就不存在了。 enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

根据Spring documentation@ContextConfiguration查找资源。因此,您可以使用结构resource创建/test文件夹,最好位于com/puut/bitcoin/dao/test/config/datasource.xml文件夹下。这样弹簧配置应该能够读取测试资源。因此,您的文件夹结构应如下所示:

/test/java - contains your test cases
/test/resources - contains your XML/configuration files