简体   繁体   English

访问testng的@BeforeTest中的spring上下文

[英]Accessing spring context in testng's @BeforeTest

I would like to register some web scopes to the spring context in my @BeforeTest method. 我想在@BeforeTest方法@BeforeTest一些Web范围注册到spring上下文中。 But it turns out that spring context is still null at that point. 但事实证明,春季上下文在那时仍然是null

The test runs fine though, if I change into @BeforeMethod . 如果我改成@BeforeMethod ,测试运行正常。 I wonder how I can access the context in @BeforeTest , because I don't want the scope registration code to be repeated for each test methods. 我想知道如何在@BeforeTest访问上下文,因为我不希望为每个测试方法重复范围注册代码。

Below are my code snippets. 以下是我的代码片段。

public class MyTest extends MyBaseTest {
    @Test public void someTest() { /*...*/ }
}

@ContextConfiguration(locations="/my-context.xml")
public class MyBaseTest extends AbstractTestNGSpringContextTests {
    @BeforeTest public void registerWebScopes() {
        ConfigurableBeanFactory factory = (ConfigurableBeanFactory)
                this.applicationContext.getAutowireCapableBeanFactory();
        factory.registerScope("session", new SessionScope());
        factory.registerScope("request", new RequestScope());
    }   

    /* some protected methods here */
}

Here's the error message when running the test: 这是运行测试时的错误消息:

FAILED CONFIGURATION: @BeforeTest registerWebScopes
java.lang.NullPointerException
    at my.MyBaseTest.registerWebScopes(MyBaseTest.java:22)

在BeforeTest方法中调用springTestContextPrepareTestInstance()

TestNG runs @BeforeTest methods before @BeforeClass methods. TestNG在@BeforeClass方法之前运行@BeforeTest方法。 The springTestContextPrepareTestInstance() is annotated with @BeforeClass and sets up the applicationContext . springTestContextPrepareTestInstance()使用@BeforeClass注释并设置applicationContext This is why the applicationContext is still null in a @BeforeTest method. 这就是为什么applicationContext@BeforeTest方法中仍然为null @BeforeTest is for wapping a tagged group of tests. @BeforeTest用于@BeforeTest一组标记的测试。 (It does not run before each @Test method, so it's a bit of a misnomer). (它不会在每个@Test方法之前运行,所以它有点用词不当)。

Instead of using @BeforeTest , you should probably use @BeforeClass (which is run once, before the first @Test in the current class). 您应该使用@BeforeClass (在当前类的第一个@Test之前运行一次),而不是使用@BeforeTest Be sure it depends on springTestContextPrepareTestInstance method, as in 确保它依赖于springTestContextPrepareTestInstance方法,如

@BeforeClass(dependsOnMethods = "springTestContextPrepareTestInstance")
public void registerWebScopes() {
    ConfigurableBeanFactory factory = (ConfigurableBeanFactory) 
            this.applicationContext.getAutowireCapableBeanFactory();
    factory.registerScope("session", new SessionScope());
    factory.registerScope("request", new RequestScope());
}   

The @BeforeMethod works, too (as you mentioned) because they run after @BeforeClass methods. @BeforeMethod也是如此(正如你所提到的),因为它们在@BeforeClass方法之后运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM