首页 > 解决方案 > 扩展 AbstractTestNGSpringContextTests 时,抽象基类中的自动装配弹簧依赖项始终为空

问题描述

我有一个抽象的测试基类,它扩展自AbstractTestNGSpringContextTests

@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@ActiveProfiles(resolver = ActiveProfileResolver.class)
public abstract class BaseTest extends AbstractTestNGSpringContextTests {
    @Autowired
    private MyActions myActions;

    @BeforeSuite(dependsOnMethods = "beforeSuite", alwaysRun = true)
    public void beforeUISuite() {
        myActions.doSomething();
    }
}

以及一个从BaseTest

public class ExampleTest extends BaseTest {
    @Test
    public void exampleTest( ){
        // Do something
    }
}

beforeUISuite方法运行时,myActions始终为空。为什么这不是由 Spring 自动装配的?测试运行时正确设置了自动连接的依赖项ExampleTest,但是beforeUISuite执行时,什么都没有连接?

如果我将myActionsprotected 和注释掉beforeUISuitemyActions则在测试运行时正确实例化。在标记的方法执行之前似乎没有连接@BeforeSuite

标签: javaspringdependency-injectionautowired

解决方案


推荐阅读