热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

我的考试班:publicclassmyTestextendsTestCase{@AutowiredBeanClassbeanObjectpublicvoidbeanTest(){C

我的考试班:

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扩展

参考:TestContext support classes

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


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注释接口无效!


推荐阅读
author-avatar
树上的女爷
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有