java @Autowired 注释无法在 JUnit 类中注入 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4746110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Autowired annotation not able to inject bean in JUnit class
提问by alpesh003
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
在beanObject.getMethod();
精确的给出空指针异常
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?
我应该如何在我的 Junit 类中实现 beanObject 字段的自动装配,以便我可以使用“BeanClass”类中的方法?
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?
简单来说.. beanClass 是一个具有某些方法的接口.. 我用
@Service("beanObject")
注释标记了那个 beanClass..banClass 是由 beanClassImpl 类实现的,它具有方法实现.. 我需要在我的 testClass 中使用这些实现来获取要比较的数据..为此我@Autowired
在我的testClass中做 beanClass beanObject..我在某处严重错误?
回答by Sean Patrick Floyd
You probably need to decorate your tests with these annotations:
您可能需要使用这些注释来装饰您的测试:
@ContextConfiguration(locations = {/* your xml locations here */})
@RunWith(SpringJUnit4ClassRunner.class)
Or, if you use JUnit 3.x, you should extend from AbstractJUnit38SpringContextTests
或者,如果您使用 JUnit 3.x,您应该从 AbstractJUnit38SpringContextTests
Reference:TestContext support classes
Update:The problem seems to be that the context file can't be found (see discussion in 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?
简单来说.. beanClass 是一个具有某些方法的接口.. 我用@Service("beanObject") 注释标记了那个 beanClass..banClass 是由 beanClassImpl 类实现的,它具有方法实现..我需要使用那些我的 testClass 中的实现来获取要比较的数据..为此,我在我的 testClass 中执行 @Autowired beanClass beanObject .. 我在某个地方出错了吗?
More Updates:
更多更新:
Don't annotate the interface, annotate the implementing class. Annotating the interface with @Service
has no effect!
不要注解接口,注解实现类。用 注释界面@Service
没有效果!