gpt4 book ai didi

java.io.FileNotFoundException : class path resource can't be opened

转载 作者:行者123 更新时间:2023-12-02 12:55:15 27 4
gpt4 key购买 nike

我从事 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 文件中,我看到未为此文件配置应用程序上下文,并且 IntelliJ 实际上提供了一些选项这样做。

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

有什么想法吗?

最佳答案

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

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

关于java.io.FileNotFoundException : class path resource can't be opened,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44447774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com