java junit autowired_java – @Autowired注释无法在JUnit类中注入bean

我的考试班:

public class myTest extends TestCase{

@Autowired

BeanClass beanObject

public void beanTest()

{

Classdata data = beanObject.getMethod();

}

}

我在行处获得空指针异常:

Classdata data = beanObject.getMethod();

beanObject.getMethod();精确地给出了nullpointer异常

我应该如何使我的Junit类中的字段beanObject自动装配,以便我可以使用“BeanClass”类中的方法?

复制自评论:

in plain terms.. beanClass is an

interface which has certain methods..

i have tagged that beanClass with

@Service("beanObject")

annotation..that banClass is

implemented by beanClassImpl class

which has the method implementations..

i need to use those implementations in

my testClass to get the data to be

compared.. for that i am doing

@Autowired beanClass beanObject in

my testClass.. m i going terribly

wrong somewhere?

解决方法:

您可能需要使用以下注释来装饰测试:

@ContextConfiguration(locations = {/* your xml locations here */})

@RunWith(SpringJUnit4ClassRunner.class)

或者,如果使用JUnit 3.x,则应从AbstractJUnit38SpringContextTests扩展

更新:问题似乎是无法找到上下文文件(请参阅注释中的讨论).

in plain terms.. beanClass is an

interface which has certain methods..

i have tagged that beanClass with

@Service(“beanObject”)

annotation..that banClass is

implemented by beanClassImpl class

which has the method implementations..

i need to use those implementations in

my testClass to get the data to be

compared.. for that i am doing

@Autowired beanClass beanObject in my

testClass.. m i going terribly wrong

somewhere?

更多更新:

不要注释接口,注释实现类.使用@Service注释接口无效!

标签:java,spring,junit

来源: https://codeday.me/bug/20191008/1872932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值