2012-04-17 26 views
5

Bazı web kapsamlarını @BeforeTest yöntemimde ilkbahar kapsamına kaydetmek istiyorum. Ancak bu noktada bahar içeriğinin hala null olduğu ortaya çıkıyor.Test bağlamında erişme @BeforeTest

@BeforeMethod olarak değiştirirseniz, sınama gayet iyi çalışıyor. Bağlam kayıt kodunun her bir test yöntemi için tekrarlanmasını istemediğim için, @BeforeTest'da içeriğe nasıl erişebileceğimi merak ediyorum.

Kod snippet'larım aşağıdadır. testi çalıştırırken

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 */ 
} 

İşte hata mesajı yok: En BeforeTest yöntemde

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

cevap

12

Çağrı springTestContextPrepareTestInstance().

+0

Enjekte edilen üyelerin '@ BeforeTest' ve hatta' @ BeforeSuite 'yöntemlerinde neden kullanılmadığına dair herhangi bir belgenin farkında mısınız? – ecbrodie

+0

10x :) daha sonra, daha sonra, çekirdek sonrası işlemci yazıcısı –

2

TestNG, @BeforeClass yöntemlerinden önce @BeforeTest yöntemlerini çalıştırır. springTestContextPrepareTestInstance(), @BeforeClass ile ek açıklamalı ve applicationContext'u ayarlıyor. Bu nedenle, yönteminde applicationContext hala null yöntemidir. @BeforeTest, etiketli bir test grubunun silinmesi içindir. (Her @Test yönteminden önce çalışmaz, bu yüzden bir yanlış adlandırma).

@BeforeTest kullanmak yerine, muhtemelen @BeforeClass (geçerli sınıftaki ilk @Test'dan önce bir kez çalıştırılır) kullanmalısınız. (Bahsettiğiniz gibi) onlar @BeforeClass yöntemlerle sonra çalıştırın çünkü çok

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

@BeforeMethod eserlerinde olduğu gibi, springTestContextPrepareTestInstance yöntemine bağlıdır emin olun.

+0

"' @ BeforeClass' (mevcut sınıftaki herhangi bir @ @ Tests'den önce çalıştırın) yazılır. ". <- Bu yanlış. [TestNG dökümantasyonunda] (http://testng.org/doc/documentation-main.html) 'de belirtildiği gibi, mevcut sınıftaki ** ilk ** test yöntemi uygulanmadan önce @ BeforeClass' çalıştırılacaktır. – JonyD

+0

@JonyD, bunu işaretlediğiniz için teşekkürler, ifadeler açık değil. Bunu önerinize göre düzenledim. – kuporific

İlgili konular