Come rendere @BeforeClass eseguire prima carichi Primavera TestContext up?
-
14-10-2019 - |
Domanda
dovrebbe essere pezzo di torta per i programmatori che utilizzano TestNG. Ho questo scenario
@ContextConfiguration(locations={"customer-form-portlet.xml", "classpath:META-INF2/base-spring.xml" })
public class BaseTestCase extends AbstractTestNGSpringContextTests {
...
@BeforeClass
public void setUpClass() throws Exception {
Ma avrei bisogno di un contesto di primavera per essere caricare dopo @BeforeClass. Io mi si avvicinò con l'override metodi AbstractTestNGSpringContextTests:
@BeforeClass(alwaysRun = true)
protected void springTestContextBeforeTestClass() throws Exception {
this.testContextManager.beforeTestClass();
}
@BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")
protected void springTestContextPrepareTestInstance() throws Exception {
this.testContextManager.prepareTestInstance(this);
}
e fare il mio metodo
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
protected void springTestContextPrepareTestClass() throws Exception {
}
Ma poi ho capito:
Causato da: org.testng.TestNGException: org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance () Non è consentito a dipendere protetti vuoto org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestClass () getta java.lang.Exception
renderlo pubblico non lo fa anche aiuto. Potrebbe per favore qualcuno menzione qui se si può fare in modo di lavoro :-) So che avrei potuto caricare il testContext manualmente, ma che non sarei così fantasia.
Funziona in questo modo, ma TestContextManager non è visibile, quindi non posso chiamare prepareTestInstance () metodo su di esso:
@Override
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
public void springTestContextPrepareTestInstance() throws Exception {
}
Soluzione
Beh ho creato su misura DependencyInjectionTestExecutionListener e ho injectDependencies sovrascritto () e fatto il mio codice di inizializzazione in là
@TestExecutionListeners( inheritListeners = false, listeners = {DITestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@ContextConfiguration(locations= "customer-form-portlet.xml")
public class BaseTestCase extends AbstractTestNGSpringContextTests {
E
public class DITestExecutionListener extends DependencyInjectionTestExecutionListener {
protected void injectDependencies(final TestContext testContext) throws Exception {
INITSTUFF();
Object bean = testContext.getTestInstance();
AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
beanFactory.initializeBean(bean, testContext.getTestClass().getName());
testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}