vendredi 1 septembre 2017

How to configure the beforeTestClass method of the AbstractTestExecutionListener class - Spring

In my project I need to prepare an environment for the execution of my integration tests, for this I use the @TestExecutionListeners annotation of spring, and I point a class, until this part is all right.

My problem is that when I'm going to give an insert in my database, in the beforeTestClass method of the AbstractTestExecutionListener insert statements do not work, in the TestExecutionListeners class I try to save a list of objects, but in my tests they are not there.

My test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContextTest.xml"})
@TransactionConfiguration(defaultRollback=true,             
transactionManager="defaultTransactionManager")
@TestExecutionListeners(listeners= {
        DependencyInjectionTestExecutionListener.class,
        TransactionalTestExecutionListener.class, 
        CriaPessoaEnvironment.class,
        }) 
public class EnvironmentTest {

    @Autowired
    private PessoaFacade PessoaFacade;

    @Test
    @Transactional
    public void verificaSeTem5cidadaosCadastrados() {
        List<Pessoa> listPessoa = pessoaFacade.listPessoa();

        System.out.println("Quantidade da lista: " + listPessoa.size());
        Assert.assertEquals(1, listPessoa.size());
    }
}

My enviromnent class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContextTest.xml"})
@TestExecutionListeners({CriaPessoaEnvironment.class})
public class CriaPessoaEnvironment extends AbstractTestExecutionListener{

    @Autowired
    private PessoaFacade pessoaFacade;

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        TransactionalTestExecutionListener     
transactionalTestExecutionListener = new TransactionalTestExecutionListener();
    transactionalTestExecutionListener.beforeTestClass(testContext);
    testContext.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);
    pessoaFacade.gravarPessoa(criaPessoa());
    }
}

Aucun commentaire:

Enregistrer un commentaire