没有定义bean命名

时间:2016-10-14 05:43:26

标签: java spring junit

我正在使用JUnit测试Spring的Component,抛出错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'wsVehicleService' is defined

这是我的测试类:

package com.zw.ws.service.impl;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/*.xml")    
public class WsVehicleServiceTest {

    @Autowired
    ApplicationContext ctx;

    @Test
    public void getHistoryAlarmInfo() throws Exception {
        try {
            String requestJson = "\"{\"VEHICLEID\":\"cae21196-cb66-4256-88a6-7cdfb23e2c78\",\"STARTTIME\":1476115200,\"ENDTIME\":1476201599,\"TYPE\":1}\"";
            WsVehicleService vehicleService = (WsVehicleService) ctx.getBean("wsVehicleService");
            vehicleService.getHistoryAlarmInfoImpl(requestJson);
        } catch (BeansException e) {
            e.printStackTrace();
        }
    }
}

这是将要测试的课程:

@Component
public class WsVehicleService {

    public List<HistoryAlarmInfo> getHistoryAlarmInfo(String requestJson) {
        try {
            return getHistoryAlarmInfoImpl(requestJson);
        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }
}

当JUnit运行到getBean时,抛出错误:No bean named 'wsVehicleService' is defined。 出了什么问题?我能做些什么来做对吗?

我尝试了什么:

添加@ComponentScan("com.zw.ws.*")以扫描WsVehicleService类。

这是日志:

    12:53:11.098]-[main]-[org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener]-{After test method: context [DefaultTestContext@58783f6c testClass = WsVehicleServiceTest, testInstance = com.zw.ws.service.impl.WsVehicleServiceTest@7577b641, testMethod = getHistoryAlarmInfo@WsVehicleServiceTest, testException = org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'wsVehicleService' is defined, mergedContextConfiguration = [MergedContextConfiguration@3a7b503d testClass = WsVehicleServiceTest, locations = '{classpath:/*.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null].}

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'wsVehicleService' is defined

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054)
    at com.zw.ws.service.impl.WsVehicleServiceTest.getHistoryAlarmInfo(WsVehicleServiceTest.java:27)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:253)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    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:147)


            12:53:11.107]-[main]-[org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener]-{After test class: context [DefaultTestContext@58783f6c testClass = WsVehicleServiceTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3a7b503d testClass = WsVehicleServiceTest, locations = '{classpath:/*.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null].}

PS:这不是XML配置,所有bean都使用自动扫描。

4 个答案:

答案 0 :(得分:5)

1)第一个解决方案:

@ContextConfiguration初始化Spring上下文,默认情况下,在与单元测试相同的包中查找Spring XML文件,文件名与带有&#39; -context.xml&#39;的类相同。作为后缀

尝试在com.mycompany.annotation包中添加WsVehicleServiceTest-context.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

      <bean id="wsVehicleService"
          class="com.mycompany.annotation.WsVehicleService">
      </bean>

</beans>

删除后的Junit测试类(&#34;类路径:/ * .xml&#34;):

package com.mycompany.annotation;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class WsVehicleServiceTest {

    @Autowired
    ApplicationContext ctx;

    @Test
    public void getHistoryAlarmInfo() throws Exception {
        try {
            WsVehicleService vehicleService = (WsVehicleService) ctx
                    .getBean("wsVehicleService");
            String hi = vehicleService.sayHi();
            System.out.println(hi);
        } catch (BeansException e) {
            e.printStackTrace();
        }
    }
}

注意:如果您想使用@ContextConfiguration("classpath:/*.xml")请务必使用

<bean id="wsVehicleService"
              class="com.mycompany.annotation.WsVehicleService">
</bean>

在任何一个* .xml文件中

2)第二个解决方案:

确保将<context:annotation-config />放入xml文件中。在这里,您不需要在xml文件中声明wsVehicleService bean。

使用@Autowired

上的WsVehicleService修改您的测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/*.xml")   
public class WsVehicleServiceTest {

    @Autowired
    ApplicationContext ctx;

    @Autowired
    WsVehicleService wsVehicleService;

    @Test
    public void getHistoryAlarmInfo() throws Exception {
        try {
            String hi = wsVehicleService.sayHi();
            System.out.println(hi);
        } catch (BeansException e) {
            e.printStackTrace();
        }
    }
}

3)第三种解决方案:

您已经提到过,您不使用任何xml配置文件,而您的异常会告诉org.springframework.beans.factory.NoSuchBeanDefinitionException

您的WsVehicleService可能不存在于上下文中的一个原因是它可能在Spring未扫描的包中定义。

确保您的WsVehicleService存在于@ComponentScan("com.mycompany.annotation")包中。

1)带注释@Configuration

的应用程序配置类
package com.mycompany.annotation;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@ComponentScan("com.mycompany.annotation")
public class AppConfig {

}

2)您的服务类

package com.mycompany.annotation;

import org.springframework.stereotype.Component;

@Component
public class WsVehicleService {
    public String sayHi(){
        return "HI !!!!!!!!!!";
    }
}

3)Junit测试类

package com.mycompany.annotation;

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

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

    @Autowired
    ApplicationContext ctx;

    @Test
    public void getHistoryAlarmInfo() throws Exception {
        try {
            WsVehicleService vehicleService = (WsVehicleService) ctx
                    .getBean("wsVehicleService");
            String hi = vehicleService.sayHi();
            System.out.println(hi);
        } catch (BeansException e) {
            e.printStackTrace();
        }
    }
}

输出:

HI !!!!!!!!!!

有关详细信息,请参阅thisthis

causes and possible solution for NoSuchBeanDefinitionException

的更多参考资料

答案 1 :(得分:4)

更改行

WsVehicleService vehicleService = (WsVehicleService) ctx.getBean(WsVehicleService .class);

@Component("wsVehicleService")
public class WsVehicleService{

//..

}

答案 2 :(得分:3)

“在以下情况下抛出没有名为'xxx'的bean”:

  • 您的bean有不同的名称。
  • 你的bean实际上并不在上下文中。

由于您没有更改bean的默认名称,因此必须是bean未正确加载。

您需要指明要扫描的组件的位置(xml config或@Configuration类),并在运行测试时加载。 我将创建一个配置类,然后在ContextConfiguration注释中添加这个新的配置类(或者如果您愿意,可以使用xml)。

@Configuration
@ComponentScan(basePackages = {"com.zw.ws"})
public class AppTestConfig {}

然后你的测试班:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AppTestConfig.class}) 
public class WsVehicleServiceTest {
...
}

您也可以使用@Autowire服务进行配置,让Spring进行接线工作。

答案 3 :(得分:0)

Spring注释代码

//main class    
//@Import(value=AppConfig.class)     
public class AppAnno {   
    public static void main(String[] args) {  
        AnnotationConfigApplicationContext ctx = new  
 AnnotationConfigApplicationContext(AppConfig.class);  
    TodayDate date = ctx.getBean("todayDate",TodayDate.class);   
System.out.println(date.getTodyDate());     
    }     
}    

@Configuration  
@ComponentScan(basePackages={"com.cb.service.anno","com.cb.dao.anno"})  

根据你的basepackage改变它

public class AppConfig {  
    @Bean 
public Date date(){    
return new Date();   
}    
}  

import java.util.Date;     
import javax.inject.Named;   
import org.springframework.beans.factory.annotation.Autowired;    
import org.springframework.stereotype.Component;   
@Component     
@Named("todayDate")//you have to add inject jar to your build path     
public class TodayDate {      
@Autowired    
    Date date;    
    public String getTodyDate(){     
    return date.toString( );    
    }     
}