@Autowired 注释无法在 JUnit 类中注入 bean
我的测试类:
public class myTest extends TestCase{
@Autowired
BeanClass beanObject
public void beanTest()
{
Classdata data = beanObject.getMethod();
}
}
我在以下行收到一个空指针异常:
Classdata data = beanObject.getMethod();
beanObject.getMethod();
精确地给出了空指针异常
我应该如何在我的 Junit 类中自动装配字段 beanObject 以便我可以使用“BeanClass”类中的方法吗?
复制自评论:
简单来说..beanClass 是一个 具有某些方法的接口.. 我已经用这个 beanClass 标记了
@Service("beanObject")
注释..banClass 是 由beanClassImpl类实现 其中有方法实现.. 我需要在中使用这些实现 我的 testClass 来获取数据 比较..为此我正在做@Autowired
beanClass 中的beanObject 我的测试班..我进展得很糟糕 哪里错了?
my test class:
public class myTest extends TestCase{
@Autowired
BeanClass beanObject
public void beanTest()
{
Classdata data = beanObject.getMethod();
}
}
I am getting a null pointer exception at line:
Classdata data = beanObject.getMethod();
the beanObject.getMethod();
precisely gives nullpointer exception
How should i make possible the autowiring of the field beanObject in my Junit class so that i can use the methods from the "BeanClass" class?
Copied from Comments:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要使用这些注释来装饰您的测试:
或者,如果您使用 JUnit 3.x,则应该从
AbstractJUnit38SpringContextTests
Reference: TestContext 支持类
更新:问题似乎是找不到上下文文件(请参阅评论中的讨论)。
更多更新:
不要注释接口,注释实现类。用
@Service
注解接口没有任何效果!You probably need to decorate your tests with these annotations:
Or, if you use JUnit 3.x, you should extend from
AbstractJUnit38SpringContextTests
Reference: TestContext support classes
Update: The problem seems to be that the context file can't be found (see discussion in comments).
More Updates:
Don't annotate the interface, annotate the implementing class. Annotating the interface with
@Service
has no effect!