Chcę użyć niestandardowego TestExecutionListener
w połączeniu z SpringJUnit4ClassRunner
, aby uruchomić konfigurację schematu Liquibase w mojej testowej bazie danych. Moja TestExecutionListener
działa dobrze, ale kiedy używam adnotacji na mojej klasie, wstrzyknięcie testowanego DAO nie działa, przynajmniej instancja jest pusta.Wstrzyknięcie testu sprężyny nie działa podczas korzystania z TestExecutionListener
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext-test.xml" })
@TestExecutionListeners({ LiquibaseTestExecutionListener.class })
@LiquibaseChangeSet(changeSetLocations={"liquibase/v001/createTables.xml"})
public class DeviceDAOTest {
...
@Inject
DeviceDAO deviceDAO;
@Test
public void findByCategory_categoryHasSubCategories_returnsAllDescendantsDevices() {
List<Device> devices = deviceDAO.findByCategory(1); // deviceDAO null -> NPE
...
}
}
Słuchacz jest dość prosta:
public class LiquibaseTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
final LiquibaseChangeSet annotation = AnnotationUtils.findAnnotation(testContext.getTestClass(),
LiquibaseChangeSet.class);
if (annotation != null) {
executeChangesets(testContext, annotation.changeSetLocations());
}
}
private void executeChangesets(TestContext testContext, String[] changeSetLocation) throws SQLException,
LiquibaseException {
for (String location : changeSetLocation) {
DataSource datasource = testContext.getApplicationContext().getBean(DataSource.class);
DatabaseConnection database = new JdbcConnection(datasource.getConnection());
Liquibase liquibase = new Liquibase(location, new FileSystemResourceAccessor(), database);
liquibase.update(null);
}
}
}
Brak błędów w dzienniku, tylko NullPointerException
w moim teście. Nie rozumiem, w jaki sposób korzystanie z mojego telefonu TestExecutionListener
wpływa na proces autowykonywania lub wstrzykiwania.
To prawda: jeśli podasz 'TestExecutionListener' za pomocą' @ TestExecutionListeners', możesz domyślnie zastąpić wszystkie domyślne TestExecutionListeners. Oczywiście funkcja ta nie jest dobrze udokumentowana. Możesz więc otworzyć problem JIRA, aby poprosić o udoskonalenie dokumentacji. ;) –
@SamBrannen: Właściwie jest to udokumentowane - być może nieco implicite. Zobacz moją zaktualizowaną odpowiedź. – nansen
Doskonale zdaję sobie sprawę z tekstu, który cytowałeś, od kiedy go napisałem. ;) Ale ... to nie opisuje dokładnie napotkanego scenariusza. Dlatego zaproponowałem, abyś otworzył bilet JIRA, aby ulepszyć dokumentację. –