2015-02-25 12 views
6

Hallo, ich versuche, Junit-Testfälle zu entfachen ... und ich benötige meinen vollen Anwendungskontext, um geladen zu werden. Der Junit-Test initialisiert jedoch nicht den vollständigen Anwendungskontext.Spring JUnit Test lädt nicht voll Anwendungskontext

Prüfklasse:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
public class MongoDbRepositoryTest { 

    @Value("${spring.datasource.url}") 
    private String databaseUrl; 

    @Inject 
    private ApplicationContext appContext; 

    @Test 
    public void testCRUD() { 
     System.out.println("spring.datasource.url:" + databaseUrl); 
     showBeansIntialised(); 
     assertEquals(1, 1); 
    } 

    private void showBeansIntialised() { 
     System.out.println("BEEEAAANSSSS"); 
     for (String beanName : appContext.getBeanDefinitionNames()) { 
      System.out.println(beanName); 
     } 
    } 

Ausgang:

spring.datasource.url:${spring.datasource.url} 
BEEEAAANSSSS 
org.springframework.context.annotation.internalConfigurationAnnotationProcessor 
org.springframework.context.annotation.internalAutowiredAnnotationProcessor 
org.springframework.context.annotation.internalRequiredAnnotationProcessor 
org.springframework.context.annotation.internalCommonAnnotationProcessor 
org.springframework.context.annotation.internalPersistenceAnnotationProcessor 
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor 
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor 

Hauptanwendungsklasse Anmerkungen:

@ComponentScan(basePackages = "com.test") 
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class }) 
@EnableMongoRepositories("com.test.repository.mongodb") 
@EnableJpaRepositories("com.test.repository.jpa") 
@Profile(Constants.SPRING_PROFILE_DEVELOPMENT) 
public class Application { ... 

Daher sollte es die ganze Frühling Bohne im Paket com.test scannen und Laden Sie sie auch in den Anwendungskontext für den Junit-Testfall. Aber aus dem Output der initalisierten Bohnen scheint das nicht zu sein.

+1

Zum einen ist Ihre Anwendungsklasse nur für ein Profil aktiv, das Sie nicht für den Test festlegen. – Steve

+0

Danke Steve bemerkt. –

Antwort

7

Sie müssen Ihre Testklasse wie folgt mit annotieren; Andernfalls ist Ihre Application Konfigurationsklasse immer deaktiviert. Aus diesem Grund sehen Sie derzeit keine Ihrer eigenen Bohnen in der ApplicationContext aufgeführt.

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@ActiveProfiles(Constants.SPRING_PROFILE_DEVELOPMENT) 
public class MongoDbRepositoryTest { /* ... */ } 

Zusätzlich Application sollte mit @Configuration mit Anmerkungen versehen werden, wie jemand anderes erwähnt wurde.

+0

Danke Sam..that mein Problem gelöst! Vielen Dank auch für Ihre Beiträge zu Spring Recipes .. es war eine meiner ersten Quellen für den Frühling! –

0

Vermissen Sie vielleicht eine @Configuration Annotation in Ihrer Application Klasse?

+0

Auch nach dem Hinzufügen von @Configration zu meiner Application.class hat es nicht funktioniert. –

+0

Da ich nicht kommentieren konnte, habe ich eine Antwort geschrieben. Bekannt für zukünftige Referenz. – rikica

Verwandte Themen