Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将@Autowired添加到我的服务层会导致Junit错误无法加载ApplicationContext?_Java_Spring_Spring Mvc_Junit - Fatal编程技术网

Java 将@Autowired添加到我的服务层会导致Junit错误无法加载ApplicationContext?

Java 将@Autowired添加到我的服务层会导致Junit错误无法加载ApplicationContext?,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,将@Autowired添加到我的服务层会导致Junit错误无法加载ApplicationContext 我的公司有一些评论代码,我们都是核心。我今天想把它添加到我的服务中,所以我把它添加到POM.XML中,然后我做了: @Autowired private CoreDao coreDao; 一旦我添加了这个,我的JUnit测试就开始出现错误 Failed to load ApplicationContext 所以我把这两行注释掉了,一切都很好。为什么添加大约两行会破坏我的测试 这是我的jUn

将@Autowired添加到我的服务层会导致Junit错误无法加载ApplicationContext

我的公司有一些评论代码,我们都是核心。我今天想把它添加到我的服务中,所以我把它添加到POM.XML中,然后我做了:

@Autowired
private CoreDao coreDao;
一旦我添加了这个,我的JUnit测试就开始出现错误

Failed to load ApplicationContext
所以我把这两行注释掉了,一切都很好。为什么添加大约两行会破坏我的测试

这是我的jUnit测试,所以我不知道为什么要看它

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestDatabaseConfig.class)
public class TestApp {


    private static Log LOGGER = LogFactory.getLog(TestApp.class.getName());

    @Autowired
    private MemberInquiryService service;

    @Test
    public void testgetMemeberRequestInformation() {

        MemberRequest inMemberRequest = new MemberRequest();

        inMemberRequest.setRequestor("cpilling04@aol.com.dev");
        MemberInquiryInformation testInfo = service.getMemeberRequestInformation(inMemberRequest);

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


    @Test
    public void testListMemberInquirys(){

        List<MemberInquiry> listMemberInquirys = service.listMemberInquirys();

        LOGGER.debug("Number of MemberInquiry(S) returned (" + listMemberInquirys.size() +")");


    }

}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestDatabaseConfig.class)
公共类测试{
私有静态日志记录器=LogFactory.getLog(TestApp.class.getName());
@自动连线
私人会员查询服务;
@试验
public void testgetMemeberRequestInformation(){
MemberRequest inMemberRequest=新建MemberRequest();
inMemberRequest.setRequestor(“cpilling04@aol.com.dev");
MemberInquiryInformation testInfo=service.getMemberRequestInformation(inMemberRequest);
Assert.assertEquals(testInfo.getFirst_Name(),“Christine”);
Assert.assertEquals(testInfo.getLast_Name(),“Pillings”);
}
@试验
public void testListMemberInquirys(){
List listMemberInquirys=service.listMemberInquirys();
LOGGER.debug(“返回的成员查询数(“+listMemberInquirys.size()+”));
}
}

我将stacktrace设置为

,原因是DataConfig没有对包含核心bean的包进行ComponentScan

例如:

@ComponentScan(basePackages= { "org.xxxx.inquiry", "org.xxxx.core" })
两种可能性:

  • 正如SJS所说,您忘记了@ComponentScan,spring无法将CoreDao自动连接到特定类型
  • 如果CoreDao是基于JPA的,那么如果persistence.xml中没有设置,则需要相应的表:

  • 我也有类似的问题,我花了几天时间才解决。最后,我用value=“create”代替value=“validate”来解决这个问题。

    您能在失败之前显示日志吗?您的
    TestDatabaseConfig
    类是否包含
    CoreDao
    bean定义?是否有完整的堆栈跟踪?您的JUnit中是否有与正常运行时相同的库?@Sotirios Delimanolis是的,我确实将CoreDao中的bean添加到了TestDatabaseConfigI的setPackagesToScan中,并将stacktrace设置为