【问题标题】:Getting error trying to test service on Spring project with junit尝试使用 junit 在 Spring 项目上测试服务时出错
【发布时间】:2012-09-11 21:43:27
【问题描述】:

我正在尝试使用 junit 在 Eclipse 中的 Spring Web Flow 项目上运行我的第一个测试,也可以从控制台使用 mvn test 运行我的第一个测试,但给了我同样的错误。

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/spring/root-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring/root-config.xml] cannot be opened because it does not exist

我检查了一下,我确实在该位置有这个文件,所以我不知道为什么 Eclipse 和 Maven 没有找到它。有人可以帮我吗...下面是我的测试课程

package org.uftwf.memberinquiry.text;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.uftwf.memberinquiry.model.MemberInquiryInformation;
import org.uftwf.memberinquiry.model.MemberRequest;
import org.uftwf.memberinquiry.service.MemberInquiryService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/WEB-INF/spring/root-config.xml")
public class TestApp {

    @Autowired
    private MemberInquiryService service;

    @Test
    public void testgetMemeberRequestInformation() {

        MemberRequest inMemberRequest = new MemberRequest();

        MemberInquiryInformation testInfo = service.getMemeberRequestInformation(inMemberRequest);

        inMemberRequest.setRequestor("cpilling04@aol.com.dev");


        Assert.assertEquals(testInfo.getFirst_Name(), "Christine");
        Assert.assertEquals(testInfo.getLast_Name(), "Pillings");
    }

}

【问题讨论】:

  • 错误很明显,你为什么要从特定的类路径文件夹加载测试上下文?让它与你的测试类相关

标签: java spring junit spring-webflow


【解决方案1】:
@ContextConfiguration(locations = {classpath:spring/root-config.xml})

或创建

TestApp-context.xml

【讨论】:

  • 在允许 TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@39e4853f] 准备测试实例时捕获异常 [org.uftwf.memberinquiry.text.TestApp@49e808ca] org.springframework.beans .factory.BeanDefinitionStoreException: IOException 从类路径资源 [WEB-INF/spring/root-config.xml] 解析 XML 文档;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [WEB-INF/spring/root-config.xml] 无法打开,因为它不存在
  • 只复制我的变体没有 [WEB-INF/spring/
  • 还有一个问题,根配置是你的应用程序的应用配置吗?如果是,请在 test/resources 文件夹中创建测试配置并导入您的应用程序配置
  • 是的,但它适用于网络项目。测试资源中的一个如何加载 web-inf 中的那个?
  • 我将它们移到了测试/资源中,它看起来更好。我现在 gettng java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException
【解决方案2】:

我更新到最新的 spring-test 和 junit,现在一切都找到了

【讨论】:

    【解决方案3】:
    @ContextConfiguration(locations = "classpath:WEB-INF/spring/root-config.xml")
    

    路径以/开头时,理解为绝对路径。

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多