2014-05-12 15 views
5

Ich bin der nächste Test ausgeführt wird:Frühling @ContextConfiguration

import static org.junit.Assert.assertEquals; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }) 
public class FloorServiceTest { 

    @Autowired 
    private FloorService floorService; 

    @Test 
    public void testFloorService() { 

     floorService.findById((long)1); 

    } 
} 

Mit der Datei applicationContext.xml unter dem Ordner /APP/src/main/resources/META-INF/spring/

Aber es scheint nicht richtig die Bohne Autowire:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.confloorapp.services.FloorServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.confloorapp.services.FloorService com.confloorapp.services.FloorServiceTest.floorService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.confloorapp.services.FloorService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
+0

Diesen Anwendungskontext posten? – chrylis

+0

Könnten Sie bitte Ihre ApplicationContext-Datei veröffentlichen. Probieren Sie 'ApplicationContext context = new ClassPathXmlApplicationContext (" Klassenpfad: /META-INF/spring/applicationContext.xml ");' innerhalb Ihrer Klasse, das könnte funktionieren –

+0

Können Sie versuchen mit: @ContextConfiguration (locations = {"classpath: /applicationContext.xml "}) – Arpit

Antwort

11

Try

@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) 

Ehrlich gesagt würde ich von der XML wegtreten und diesen Weg gehen. ändern

@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }) 

zu

@ContextConfiguration(classes = { FloorServiceTestConfig.class }) 

Und die über Klasse erstellen

@Configuration 
public class FloorServiceTestConfig 
{ 
    @Bean 
    public FloorService floorService() 
    { 
      return new FloorService(); 
    } 
} 

Auf diese Weise, wenn Sie Ihre Bohnen für Klasse verspotten müssen bist du nicht es sieht aus wie unten Testen

@Configuration 
public class FloorServiceTestConfig 
{ 
    @Bean 
    public FloorService floorService() 
    { 
      return Mockito.mock(FloorService.class); 
    } 
} 
+0

@ContextConfiguration (Speicherorte = {" classpath:/META-INF/spring/applicationContext.xml "}) doen hat nicht funktioniert :-( – darkZone

1

Die obige Idee von @Configuration funktioniert gut. Dafür müssen Sie Ihre Klassen mit annotieren. Dies ist keine nette Idee, wenn es darum geht, offline zu testen, d. H. Wenn in Ihrer Organisation Testfälle nicht zur gebauten Zeit ausgeführt werden. In diesem Fall wird @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) eine nette Idee sein.

Für diese Methode verwenden, kümmern sich um die folgenden:

  1. Überprüfen Sie Ihre Classpath in .classpath Datei Ihres Projekts.

  2. Wenn Sie den Pfad applicationContext relativ zu Classpath nicht schreiben können.

Sie können eine weitere Datei als applicationContextTest Ordner in Ihrem Testfall Ordner gehalten und dann kann man @ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})

0

In Ihrem Fall verwenden, sollten Sie verwenden:

@ContextConfiguration(locations = "file:src/main/resources/META-INF/spring/applicationContext.xml") 

Ein weiterer Hinweis, Ich sehe, dass Sie Ihre Produktion applicationContext.xml in Ihrer Testumgebung verwenden, IMHO ist es keine gute Praxis, versuchen Sie, eine bestimmte für das Testen wie applicationContext-test.xml zu verwenden, damit Sie mit Ihrem Testkontext spielen können ohne Ihre Produktionsumgebung zu beeinträchtigen.

Verwandte Themen