junit 4 测试死活找不到classpath

本文介绍了一种在Spring集成JUnit4测试时遇到的ClassPath找不到问题,并提供了一个解决方案,即通过调整ClassPath配置使其能递归查找配置文件。

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

 今天在项目里用junit4集成spring做测试,以前用完全没问题,但是今天用就是找不到classpath,搞得我很无语,但是现在做一个折中办法先记录下来

 

以前使用代码:

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 AA.AA.Test;
  
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration({"classpath:beans.xml"})  
public class T {  
  
    @Autowired  
    private Test test;  
      
    @org.junit.Test  
    public void getTest(){  
    	test.hi();
    }  
}  

 

现在折中办法:

 

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 AA.AA.Test;
  
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration({"classpath*:/beans.xml"})  
public class T {  
  
    @Autowired  
    private Test test;  
      
    @org.junit.Test  
    public void getTest(){  
    	test.hi();
    }  
}  

 

大家应该可以看出,唯一不同的是classpath的处理,这里让它递归去找beans文件。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值