作者:树上的女爷 | 来源:互联网 | 2023-08-29 20:29
我的考试班: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注释接口无效!