在 junit test下,(不开启tomcat)读取WEB-INF/下的applicationContext.xml文件

本文介绍了解决在JUnit环境下Spring配置文件放置在WEB-INF目录下时出现的路径问题。通过使用FileSystemXmlApplicationContext并指定绝对路径的方式成功加载配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做一个项目,要测试一些类,所有就有了在junit test 下测试的想法,刚开始的时候spring配置文件放在classpath内没有问题,可是放到WEB-INF下就不行了,提示找不到,后来花了一晚上找资料,问先辈,终于找到解决方法了,代码:

1  BeanFactory factory = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext-count.xml");
FileSystemXmlApplicationContext 内部应该是实现了System.getProperty("user.dir")方法,判断传入的字符串是相对路径,还是绝对路径,如果是相对路径则会加入System.getProperty("user.dir"),获取该工程的路径进行拼接,从而进行加载

转载于:https://www.cnblogs.com/i-_-i/archive/2009/01/05/1369742.html

<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> <version>1.6.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-engine --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-engine</artifactId> <version>1.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies>这是我的maven依赖,怎么将junit4 变成junit5
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值