NullPoinerException自动装配的bean - Java,Spring

时间:2018-01-26 11:35:37

标签: java spring

我无法弄清楚出了什么问题:/当我运行NullPointerException时,我得到autowiredServiceTest()。我认为配置应该是正确的,在IDE中我可以点击依赖关系,当我手动拉appContext时,bean工作,但是下面的代码我得到错误:

上下文:appCtx-watchdog-test-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:component-scan base-package="cz.aegis.watchdog" />
    <context:annotation-config />

    <bean id="helloWorldService" class="cz.aegis.watchdog.server.impl.HelloWorldServiceImpl" />

</beans>

接口:

package cz.aegis.watchdog.api.model;

public interface HelloWorldService {
    public boolean getTrue();
    public String getString();
}

默认地将Impl:

package cz.aegis.watchdog.server.impl;

import cz.aegis.watchdog.api.model.HelloWorldService;

public class HelloWorldServiceImpl implements HelloWorldService {
    public boolean getTrue() {
        return true;
    }

    public String getString(){
        return "helloWorld";
    }
}

测试类:

import cz.aegis.util.security.server.UserServiceHibernate;
import cz.aegis.watchdog.api.DtoTranslator;
import cz.aegis.watchdog.api.dto.MonitorTestDto;
import cz.aegis.watchdog.api.model.HelloWorldService;
import cz.aegis.watchdog.api.model.MonitorTest;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;

import java.sql.SQLException;

import org.testng.Assert;
import org.testng.annotations.Test;

@ContextConfiguration(locations = {"classpath:appCtx-watchdog-test-server.xml"})
public class Testing {

    @Autowired
    private HelloWorldService helloWorldService;

    @Test
    public void autowiredServiceTest() {
        System.out.println(helloWorldService.getString());
        Assert.assertTrue(helloWorldService.getTrue());
    }
}

错误:

java.lang.NullPointerException
at Testing.autowiredServiceTest(Testing.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1172)
at org.testng.TestRunner.privateRun(TestRunner.java:757)
at org.testng.TestRunner.run(TestRunner.java:608)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1158)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1083)
at org.testng.TestNG.run(TestNG.java:999)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

1 个答案:

答案 0 :(得分:0)

解决了,刚补充说: 扩展AbstractTestNGSpringContextTests