@Autowired annotation not able to inject bean in JUnit class

佐手、 提交于 2019-12-19 10:17:17

问题


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?


回答1:


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

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?

More Updates:

Don't annotate the interface, annotate the implementing class. Annotating the interface with @Service has no effect!



来源:https://stackoverflow.com/questions/4746110/autowired-annotation-not-able-to-inject-bean-in-junit-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!