2011-09-01 137 views
1
@Component 
public class AnnotationTest extends TestCase{ 
    ServiceTest serviceTest; 
    @Autowired(required=false) 
    public void setServiceTest(ServiceTest serviceTest) { 
     this.serviceTest = serviceTest; 
    } 
    public void testAnnotation() { 
     new ClassPathXmlApplicationContext(new String[]{"/com/test/spring/ioc/beans.xml"}); 
     serviceTest.annotationTest("testName", "testPassword"); 
    } 

错误:弹簧注释的NullPointerException

Exception in thread "main" java.lang.NullPointerException 
    at com.test.spring.ioc.AnnotationTest.invokeIoC(AnnotationTest.java:16) 
    at com.test.spring.ioc.AnnotationTest.main(AnnotationTest.java:13) 

服务类:

@Service 
public class ServiceTestImpl implements ServiceTest{ 
    @Autowired 
    AnnotationDAO annotationDAO; 
    public List<String> annotationTest(String name, String pssword) {  
     return annotationDAO.annotationTest(name, pssword); 
    } 
} 

DAO类:

@Repository("AnnotationDAO") 
public class AnnotationDAOImpl implements AnnotationDAO{ 

    public List<String> annotationTest(String name, String pssword) { 
     List<String> list = new ArrayList<String>(); 
     list.add("Test 1"); 
     list.add("Test 2"); 
     return list; 
    } 
} 

beans.xml中:

<bean id="service" class="com.test.spring.ioc.service.ServiceTestImpl"/>  
    <context:annotation-config /> 
     <context:component-scan base-package="com.test.spring" /> 

我该如何解决这个问题?

编辑:

它被给予警告消息:

WARNING: Autowired annotation is not supported on static methods: public static void 
com.test.spring.ioc.AnnotationTest.setServiceTest(com.test.spring.ioc.service.ServiceTest) 

如上通过添加@Component I所示移动到JUnit测试而不是Java主要方法(我不知道这是必需)。 现在我得到新的错误为:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'annotationTest': Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
com.test.spring.ioc.service.ServiceTest com.test.spring.ioc.AnnotationTest.serviceTest; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
unique bean of type [com.test.spring.ioc.service.ServiceTest] is defined: expected single 
matching bean but found 2: [service, serviceTestImpl] at  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProce ssPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) 
+0

我没有意识到这是进入单元测试,这可能会帮助http://stackoverflow.com/questions/6546871/spring -junit-testing – beny23

回答

2

问题是,你正在定义同一个bean两次:

一旦明确:

<bean id="service" class="com.test.spring.ioc.service.ServiceTestImpl"/>  

一旦通过组件扫描:

<context:component-scan base-package="com.test.spring" /> 

去掉它们的方法之一,它会工作。

BTW:你不需要实例测试时自己的容器,只要用Spring TestContext Framework

@RunWith(SpringJUnit4ClassRunner.class) 
// ApplicationContext will be loaded from "/applicationContext.xml" 
// and "/applicationContext-test.xml" 
// in the root of the classpath 
@ContextConfiguration({"/applicationContext.xml", 
         "/applicationContext-test.xml"}) 
public class MyTest { 
    // class body... 
} 

,当然还有使用JUnit 4。X你不应该再从TestCase的扩展,而是使用注解的方法代替:

@Test 
public void testAnnotation() { 
    serviceTest.annotationTest("testName", "testPassword"); 
} 
+0

它抛出java.lang.Exception:没有可运行的方法。但我有注解的方法测试 – TechFind

+0

@ kasim你使用Junit 4.x吗? –

+0

如果我扩展了JUnit的TestCase或reomve它,我没有得到任何可运行的方法 – TechFind

1

AnnotationTest类不被春天实例化,所以Spring从未有机会注入serviceTest依赖性:

你可以明确地得到您的bean:

public static void main(String[] args) { 
    ApplicationContext ctx = 
     new ClassPathXmlApplicationContext(
      new String[]{"/com/test/spring/ioc/beans.xml"}); 
    serviceTest = ctx.getBean(ServiceTest.class); 
    invokeIoC(); 
} 

,或者你可以告诉与@Component注解你AnnotationTest类,以一个实例将由component-scan创建。请注意,虽然我认为这将有点破解,因为您将实例化一个对象并且从不使用它,所以我会废除主要的自动装配并使用getBean ...

编辑:

为了做一个静态字段的自动连接,你也不得不愚弄春天以为serviceTest实际上不是静态的(再次,我不认为这是一个好主意):

@Component 
public class AnnotationTest { 
    static ServiceTest serviceTest; 

    ... 

    @Autowired 
    public void setServiceTest(ServiceTest serviceTest) { 
     AnnotationTest.serviceTest = serviceTest; 
    } 
} 
+0

我试过所有这些,同样的错误来了。 – TechFind