2016-10-17 2 views
0

Ich habe kleine Testklasse für meine einfache Anwendung gemacht.Unit Testing - Spring

Ich habe einige Probleme mit ApplicationContext und kann nicht einige Klassen autowire.

Hier ist mein Test-Klasse:

package com.myapp.adminpanel.test; 


import com.myapp.adminpanel.service.CityService; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.support.AnnotationConfigContextLoader; 


import static org.junit.Assert.*; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) 
public class CityServiceTest { 

    @Configuration 
    static class ContextConfiguration { 

     @Bean 
     public CityService getCityService() { 
      return new CityService(); 
     } 
    } 

    @Autowired 
    private CityService cityService; 

    @Test 
    public void testServicePackage() { 
     assertEquals("com.logate.adminpanel.service.CityService",this.cityService. getClass().toString()); 
    } 
} 

Der folgende Code: java.lang.IllegalStateException: Failed to load ApplicationContext

jemand mir mit diesem helfen?

Vielen Dank.

Stapelüberwachung:

java.lang.IllegalStateException: Fehler Application

at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94) 
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72) 
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getCityService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.logate.adminpanel.repository.CityRepository com.logate.adminpanel.service.CityService.cityRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.logate.adminpanel.repository.CityRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125) 
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) 
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109) 
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261) 
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68) 
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) 
... 29 more 
+0

die volle stacktrace Hinzufügen ... Eine Sache ist Ihr 'ContextConfiguration' Klasse machen' öffentlich statt Standard. Und entfernen Sie das Argument 'loader' aus der Annotation @ContextConfiguration. –

Antwort

0

Es ist nicht Ihren Test jede Konfiguration verwendet gerichtet haben zu laden. Say Kontextkonfiguration welche Konfiguration zu verwenden:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = ContextConfiguration.class) 
public class CityServiceTest { 

Es ist besser, Ihre ContextConfiguration etwas weniger irreführend (MyTestConfig usw.)

Zweiter Ansatz umbenennen, können Sie Ihren Test als Konfiguration mit Anmerkungen versehen:

Aber ich sehe keinen Sinn darin, weil Sie Ihre Bean-Deklaration in anderen Tests nicht wiederverwenden können und Sie können Objekt nur mit Neu erstellen:

private CityService cityService = new CityService(); 

So ist die beste Wahl, um Ihre config separate Klasse extrahieren und übergeben es an ContextConfiguration als Klassen Argument

+0

Ich habe neue Klasse im Config-Ordner erstellt und 'Configuration'-Anmerkung darin eingefügt. Außerdem habe ich die Methode getCityService mit der Bean-Bezeichnung erstellt. Dann habe ich in meiner Testklasse einen Zusatz 'ContextConfiguration' (classes = TestConfig.class) erstellt und mein CityService-Objekt autowire, aber ich habe denselben Fehler. –

+0

geben Sie bitte stacktrace –

+0

Sie müssen CityRepository Bean deklarieren, verursachen es transitive Abhängigkeit. Sie können das Autowire-Verfahren aktivieren, indem Sie @ComponentScan (basePackages = {"com.logate.adminpanel.repository"}) zu Ihrer Testkonfiguration hinzufügen –