java – Spring @ContextConfiguration
发布时间:2020-09-21 14:39:23  所属栏目:Java  来源:互联网 
            导读:我正在进行下一个测试: import static org.junit.Assert.assertEquals;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframew
                
                
                
            | 
                         我正在进行下一个测试: 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);
    }
} 
 使用文件applicationContext.xml下的文件夹/ APP / src / main / resources / META-INF / spring / 但似乎没有正确地自动装配bean: 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)}
解决方法尝试@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) 
 老实说,我会离开xml并走这条路. @ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }) 
 至 @ContextConfiguration(classes = { FloorServiceTestConfig.class }) 
 并创建关于类 @Configuration
public class FloorServiceTestConfig
{
    @Bean
    public FloorService floorService()
    {
          return new FloorService();
    }
} 
 这种方式当你需要为类来模拟你的bean时,你没有测试它,如下所示 @Configuration
public class FloorServiceTestConfig
{
    @Bean
    public FloorService floorService()
    {
          return Mockito.mock(FloorService.class);
    }
}                        (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
推荐文章
            站长推荐
            - javafx – 在已经缩放的节点上的枢轴点进行缩放
 - 如何将SimpleDateFormat更改为jodatime?
 - java 中HashMap、HashSet、TreeMap、TreeSet判断
 - java – 使用MockRestServiceServer模拟REST调用
 - Java 反射机制知识详细介绍及总结
 - Spring MVC过滤器-登录过滤的代码实现
 - java – eclipse jsp – 没有颜色,代码完成和错误
 - Java Spring Controller 获取请求参数的几种方法
 - java – 应用程序不是冰淇淋三明治兼容
 - 浅谈java中BigDecimal的equals与compareTo的区别
 
热点阅读
            