junit4测试ssh项目为什么我就是导不进去配置文件???

qq_35842815 2016-08-17 04:35:04
这个问题我已经研究一天了 就是导不进去 我真服了!

这是报的错:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/conf/init.properties] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [WEB-INF/conf/init.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:182)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:591)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:565)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:400)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.ceb.test.EnvironmentMessageServiceImplTest.setUp(EnvironmentMessageServiceImplTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


加载配置文件的代码
package com.ceb.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ceb.bean.EnvironmentMessage;
import com.ceb.service.impl.EnvironmentMessageServiceImpl;

public class EnvironmentMessageServiceImplTest {
public static EnvironmentMessageServiceImpl ems;
@BeforeClass
public void setUp() throws Exception {
System.out.println("加载配置文件");
String[] a = {"b.cfg.xml","ac.xml"};
ApplicationContext ac =
new ClassPathXmlApplicationContext
(a);
System.out.println("加载配置文件成功");
ems = (EnvironmentMessageServiceImpl) ac.getBean("environmentMessageService");
}




配置文件肯定没有问题啊 项目都是可以跑的
...全文
414 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
missMeyo 2016-08-18
  • 打赏
  • 举报
回复
配置文件相对于classpath,默认src和classes进行相对路径。
qq_35842815 2016-08-18
  • 打赏
  • 举报
回复
引用 6 楼 twx843571091 的回复:
java.io.FileNotFoundException: class path resource [WEB-INF/conf/init.properties] cannot be opened because it does not exist 1、可能路径写错了
是我哪个路径错了? 导入junit的路径错了 还是配置文件里init.properties的路径错了?应该都没错的呀。。
qq_35842815 2016-08-18
  • 打赏
  • 举报
回复
引用 7 楼 qnmdcsdn 的回复:
[quote=引用 3 楼 qq_35842815 的回复:] [quote=引用 2 楼 qnmdcsdn 的回复:] init.properties这个文件是个什么鬼,在哪
<bean id="p_init"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="\WEB-INF\conf\init.properties"></property>
	</bean>
在配置文件里面就配置 就在这个路径 他却说不存在[/quote] 加classpath、\ 改成 /,总之各种试试吧[/quote] / \这两种我都试过了 应该是没影响 加classpath的话是写绝对路径?
qq_35842815 2016-08-18
  • 打赏
  • 举报
回复
引用 5 楼 qq_21733941 的回复:
可能原因: ①文件路径放错了②配置文件引入的时候写错了
init.properties 这个文件的路径在 配置文件里有指定,它确实在指定的web-inf路径下, 我往junit里面导入配置文件的路径应该没错吧?
今天晴 2016-08-18
  • 打赏
  • 举报
回复
也可以试试不放在WEB-INF下
  • 打赏
  • 举报
回复
引用 3 楼 qq_35842815 的回复:
[quote=引用 2 楼 qnmdcsdn 的回复:] init.properties这个文件是个什么鬼,在哪
<bean id="p_init"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="\WEB-INF\conf\init.properties"></property>
	</bean>
在配置文件里面就配置 就在这个路径 他却说不存在[/quote] 加classpath、\ 改成 /,总之各种试试吧
  • 打赏
  • 举报
回复
java.io.FileNotFoundException: class path resource [WEB-INF/conf/init.properties] cannot be opened because it does not exist 1、可能路径写错了
qq_21733941 2016-08-18
  • 打赏
  • 举报
回复
可能原因: ①文件路径放错了②配置文件引入的时候写错了
qq_35842815 2016-08-18
  • 打赏
  • 举报
回复
大神在哪里啊
qq_35842815 2016-08-18
  • 打赏
  • 举报
回复
引用 2 楼 qnmdcsdn 的回复:
init.properties这个文件是个什么鬼,在哪
<bean id="p_init"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="\WEB-INF\conf\init.properties"></property>
	</bean>
在配置文件里面就配置 就在这个路径 他却说不存在
xitao_java 2016-08-18
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
init.properties这个文件是个什么鬼,在哪
qq_35842815 2016-08-17
  • 打赏
  • 举报
回复
求大神救救我!!!!!!!!!!!!

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧