Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 在Spring Junit测试用例中获取jdbcTemplate null_Maven_Spring Mvc_Junit4 - Fatal编程技术网

Maven 在Spring Junit测试用例中获取jdbcTemplate null

Maven 在Spring Junit测试用例中获取jdbcTemplate null,maven,spring-mvc,junit4,Maven,Spring Mvc,Junit4,我正在为Spring3RESTful服务编写junit测试用例。当我试图将其作为junit执行时,我得到的JdbcTemplate为null。我不确定我在哪里犯了错误。请帮我摆脱这个 LoginServiceImpl.java文件 private NamedParameterJdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { jdbcTemplate = new

我正在为Spring3RESTful服务编写junit测试用例。当我试图将其作为junit执行时,我得到的JdbcTemplate为null。我不确定我在哪里犯了错误。请帮我摆脱这个

LoginServiceImpl.java文件

private NamedParameterJdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    System.out.println("--------------"+jdbcTemplate.toString());
}

private static Map<String, AuthToken> tokenHash = new ConcurrentHashMap<String, AuthToken>();

private static String authTokenDetailsSql = "select * from authtoken where token = :token";

@Override
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public ServiceBean newAccount(@RequestBody Registration registration) {
    String newAccountSql = "INSERT INTO account (email,password,name) VALUES (:email,:password,:name)";
    ServiceDataBean<AuthToken> retBean = new ServiceDataBean<AuthToken>();
    try {
        System.out.println("register service calling.....");
        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
        namedParameters.addValue("email", registration.getEmail());
        messageDigest = MessageDigest.getInstance("MD5");
        byte[] md5 = new byte[64];
        messageDigest.update(registration.getPassword().getBytes("iso-8859-1"), 0, registration.getPassword().length());
        md5 = messageDigest.digest();
        namedParameters.addValue("password", convertedToHex(md5));
        namedParameters.addValue("name", registration.getName());
        GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

        // TODO what to do with the updInt also check it's not -1
        int updInt = jdbcTemplate.update(newAccountSql, namedParameters, generatedKeyHolder);
        long accountId = (Long) generatedKeyHolder.getKeys().get("GENERATED_KEY");
        registration.getDevice().setOwner(registration.getId());
        fotoframz.register(registration.getDevice());
        Login login = new Login();
        login.setEmail(registration.getEmail());
        login.setPassword(registration.getPassword());
        login.setDevice(registration.getDevice());
        retBean = (ServiceDataBean<AuthToken>) this.login(login);
        System.out.println("form register");
    } catch (Throwable e) {
        retBean.setStatusCode("001");
        e.printStackTrace();
    }
    return retBean;
}

登录服务测试 }
任何需要在测试类中修改的内容,请让我知道..

在您的测试中,
LoginServiceImpl loginServiceObj=new LoginServiceImpl()
不是由spring实例化的,因此不会应用Annotions。您需要自动连接它,或者以其他方式注入它。Spring3.2使这类东西非常容易使用

我其余的回答仍然是很好的再见: 您尚未在java代码中声明或实例化jdbctemplate。您还没有在xml文件中定义它

你需要这个

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dateSource"/>
        </property>
    </bean>

正如@NimChimpsky提到的,您需要在bean xml文件中定义
jdbcTemplate
,然后在实例变量中也可以这样做

@Autowired
private JdbcTemplate jdbcTemplate;

感谢您的即时回复。.正常的应用程序工作正常,但对于junit only,我得到的jdbctemplate为null。.我猜@user1441341只是没有将其包含在代码段中,否则他将得到编译错误。@user1441341您正在使用new关键字创建ejdbc模板,don;我不能那样做。此外,将数据访问放在控制器以外的其他位置。我推荐sprign 3.2,它使测试更容易。我们使用的是spring 3.1.0.RELEASE版本。@Nimchinpsky,非常感谢。这是一件小事,但我没有找到它:)向我们展示您的测试代码,您的junit似乎没有初始化您的SpringbeanXML。如果jdbctemplate为null,则表示未调用您的
setDatasource
方法;不会解析依赖项。您需要像@Autowired loginservice impl loginServiceObj;在单元测试课上。@spiritwalker,非常感谢你的回答。它现在开始工作了。
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dateSource"/>
        </property>
    </bean>
   @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
@Autowired
private JdbcTemplate jdbcTemplate;