Spring error in Cucumber java.lang.IllegalAccessError: tried to access class

Spring error in Cucumber java.lang.IllegalAccessError: tried to access class

我有一个黄瓜测试,当我试图获取一个 bean 时,它抛出一个非法访问错误。我还没弄明白为什么这不行。

@Given('^test$')
public void myTest(){
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppInjector.class);
    MyBean bean = ctx.getBean(MyBean.class)
}

java.lang.IllegalAccessError: tried to access class MyBean from StepDef

解决方法是添加

@ContextConfiguration(classes = AppInjector.class)

到class的顶部,然后使用@Inject拉入依赖。