2014-05-12 18 views
5

sonraki testini çalıştırıyorum: klasörün /APP/src/main/resources/META-INF/spring/Bahar @ContextConfiguration

altında dosyanın applicationContext.xml ile

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); 

    } 
} 

Ama doğru fasulye autowire görünmemektedir:

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

Bu uygulama içeriğini gönder? – chrylis

+0

ApplicationContext dosyanızı gönderebilir misiniz? deneyin 'ApplicationContext bağlam = new ClassPathXmlApplicationContext ("sınıf yolu: /META-INF/spring/applicationContext.xml"); sınıfınızın içinde' , yani sen deneyebilir miyiz –

+0

işe yarayabilecek: @ContextConfiguration (konumları = { "sınıf yolunda: /applicationContext.xml "}) – Arpit

cevap

11

deneyin

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

Dürüstçe xml'den uzaklaşıp bu rotaya giderdim. Değişim

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

Ve

@ContextConfiguration(classes = { FloorServiceTestConfig.class }) 

yaklaşık sınıf

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

Eğer bunu test etmezsiniz sınıf için fasulyenin alay gerekir Bu şekilde oluşturmak

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

@ContextConfiguration (konumlar = {" classpath:/META-INF/yayın/applicationContext.xml "}) işe yaramadı :-( – darkZone

1

Yukarıda verilen @Configuration fikri güzel çalışıyor. Ancak bunun için @Configuration ile sınıflarınıza not eklemeniz gerekiyor. Çevrimdışı test etme söz konusu olduğunda, yani kuruluşunuzda test vakası yapılmadığı zaman, bu, iyi bir fikir değildir. Bu durumda, @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) ile gitmek güzel bir fikir olacaktır.

bu yöntemi kullanarak, aşağıdaki özen:

  1. projenizin .classpath dosyada sınıf yolunu kontrol edin.

  2. Sınıf yoluna göre applicationContext yolunu yazamıyorsanız. ,

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

    başka ipucu: applicationContextTest klasör test durumu klasöründe tutulur olarak

tek bir dosya daha var ve sonra bir sen kullanmalısınız Senin durumunda @ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})

0

kullanabilirsiniz Test ortamınızda applicationContext.xml üretiminizi kullandığınızı görüyorum, IMHO iyi bir uygulama değil, applicationContext-test.xml gibi test için özel bir tane kullanmayı deneyin, böylece test içeriğinizle oynayabilirsiniz üretim ortamınıza zarar vermeden.