2015-08-29 12 views
7

Po stwierdzeniu, że fasola szparagowa nie jest podłączona do klasy testowej, powodując wyjątki nullpointer. Próbowałem zminimalizować problem i stwierdziłem, że prawdopodobnie nie jest to część Cassandra, ale zamiast tego obecność adnotacji @TestExecutionListeners wraz z rozszerzoną klasą AbstractTestExecutionListener.Adnotacja TestExecutionListeners zapobiega zbrojeniu fasoli sprężystej w

org.springframework:spring-core:4.2.0.RELEASE (Also fails with 3.2.14.RELEASE). 
org.springframework:spring-test:4.2.0.RELEASE 
junit.junit:4.11 

JVM vendor/version: Java HotSpot(TM) 64-Bit Server VM/1.8.0_40 
MAC OS X 10.10.5 

mój TestClass wygląda następująco:

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners({ AppTestListener.class }) <-- OK when removed 
@ContextConfiguration(classes = { TestConfiguration.class }) 

public class MyTest { 
    @Autowired 
    private MyService myService; 

    @Test 
    public void testMyService() { 
    Assert.assertNotNull(myService); 
    Assert.assertEquals("didit", myService.doIt()); 
    } 
} 

AppTestListener:

public class AppTestListener extends AbstractTestExecutionListener { 
    @Override 
    public void beforeTestMethod(TestContext testContext) throws Exception { 
    System.out.println("test"); 
    } 
} 

Nic specjalnego w klasie konfiguracji (i to również nie do xml konfiguracji):

@Configuration 
public class TestConfiguration { 
    @Bean 
    public MyService myService() { 
    return new MyService(); 
    } 
} 

Kiedy Usuwam adnotację @TestExecutionListeners w MyTest, ponieważ test kończy się zgodnie z oczekiwaniami, ale pozostawienie tej adnotacji powoduje niepowodzenie unieważniania na assertNotNull. Co się dzieje?

Odpowiedz

8

Na początek przykład jednostki cassandra nie jest tak naprawdę dobrym przykładem, ponieważ prowadzi do problemów podobnych do napotkanych.

Kiedy usunąć adnotację @TestExecutionListeners w MyTest testy wykończeniach zgodnie z oczekiwaniami, ale pozostawiając tę ​​adnotację czyni unittest nie na assertNotNull. Co się dzieje?

Kiedy deklarujesz @TestExecutionListeners(AppTestListener.class) w klasie testowego, który nie rozciąga innej grupy testowej uwagami z @TestExecutionListeners jesteś skutecznie informując Wiosna załadować tylko swój AppTestListener, gdy rzeczywiście chcesz użyć AppTestListener w połączeniu z domyślnym słuchacze ze Spring (np. DependencyInjectionTestExecutionListener, który dodaje obsługę iniekcji fasoli od ApplicationContext).

Aby uzyskać szczegółowe informacje, przeczytaj całą sekcję podręcznika wiosna w sekcji TestExecutionListener configuration.

Oto jak rozwiązać problem.

Przed Spring Framework 4,1

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
@TestExecutionListeners({ 
    CassandraUnitTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class, 
    DirtiesContextTestExecutionListener.class, 
    TransactionalTestExecutionListener.class 
}) 
@CassandraUnit 
public class MyCassandraUnitTest { 

    @Test 
    public void xxx_xxx() { 
    } 
} 

Po Spring Framework 4,1

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
@TestExecutionListeners(
    listeners = CassandraUnitTestExecutionListener.class, 
    mergeMode = MERGE_WITH_DEFAULTS 
) 
@CassandraUnit 
public class MyCassandraUnitTest { 

    @Test 
    public void xxx_xxx() { 
    } 
} 

Pozdrawiam,

Sam (autor Wiosna TestContext ramowego)

p.s. Stworzyłem issue for Cassandra Unit, tak aby adres ten był w ich przykładach.