1

更新 25-11-2012

嗨,看来我的上下文文件位于错误的“类路径”中,它们必须位于测试类路径中。我通过在 src/test/ 文件夹中添加一个名为 resources 的文件夹来解决这个问题。在这里,我可以放置我的应用程序上下文。我还从测试中删除了@TestExecutionListeners。我还需要为 javax/servlet/serlvetException 等添加一些依赖项到我的 pom 文件中。如果有人感兴趣,我的代码可以在Github的 issue9 分支下查看。


问题[已解决]

我使用 spring 3 制作了一个简单的应用程序。我制作了一个服务类,它使用 DAO 类通过使用简单的 JDBC 模板将事物映射到数据库和从数据库映射。

我的问题是,当我尝试对这些类进行集成测试时,它返回 NullPointerException(参见 Stacktrace)。这真的很烦人,因为我似乎找不到答案,所以我希望你们能帮助我。下面我发布了我所有的不同文件。

堆栈跟踪

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.115 sec <<< FAILURE!
test(dk.martinrohwedder.blog.test.ArticleServiceTest)  Time elapsed: 0.009 sec  <<< ERROR!
java.lang.NullPointerException
at dk.martinrohwedder.blog.service.ArticleService.getFiveLastArticles(ArticleService.java:30)
at dk.martinrohwedder.blog.test.ArticleServiceTest.test(ArticleServiceTest.java:57)
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:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
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:601)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

文章服务测试

package dk.martinrohwedder.blog.test;

import dk.martinrohwedder.blog.domain.Article;
import dk.martinrohwedder.blog.service.ArticleService;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
/**
 *
 * @author Martin Rohwedder
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:test-context.xml"})
@TestExecutionListeners
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class ArticleServiceTest {

    @Autowired(required = true)
    private ArticleService articleService;

    //private EmbeddedDatabase db;

    @Before
    public void setUp() {
        //EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        //db = builder.setType(EmbeddedDatabaseType.HSQL).setName("blog").addScript("classpath:create-db.sql").addScript("classpath:test-data.sql").build();
        //articleService.getArticleDao().setDataSource(db);
        //articleService = new ArticleService();
    }

    @After
    public void tearDown() {
        //articleService.getArticleDao().setDataSource(null);
        //db.shutdown();
        //articleService = null;
    }

    @Test
    public void test()
    {
        ArrayList<Article> articles = articleService.getFiveLastArticles();

        int number = articles.size();
        assertEquals(3, number);
    }

}

文章服务

package dk.martinrohwedder.blog.service;

import dk.martinrohwedder.blog.domain.Article;
import dk.martinrohwedder.blog.repository.ArticleDao;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 *
 * @author Martin Rohwedder
 * @since 22-11-2012
 * @version 1.0
 */
@Service("articleService")
public class ArticleService {

    @Autowired(required = true)
    private ArticleDao articleDao;

    public ArticleDao getArticleDao() {
        return articleDao;
    }

    public void setArticleDao(ArticleDao articleDao) {
        this.articleDao = articleDao;
    }

    public ArrayList<Article> getFiveLastArticles()
    {
        ArrayList<Article> articles = (ArrayList) articleDao.selectAllArticles();

        if (articles.size() > 5) {
            articles = (ArrayList) articles.subList(articles.size() - 6, articles.size());
        }

        return articles;
    }

}

文章道

package dk.martinrohwedder.blog.repository;

import dk.martinrohwedder.blog.domain.Article;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
/**
 *
 * @author Martin Rohwedder
 * @since 22-11-2012
 * @version 1.0
 */
@Repository
public class ArticleDao implements IArticleDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired(required = true)
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public List<Article> selectAllArticles() {
        String sql = "select article_id, headline from article";
        return this.jdbcTemplate.query(sql, new ArticleMapper());
    }

    @Override
    public Article selectArticle(int articleId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insertArticle(Article article) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updateArticle(Article article) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteArticle(int articleId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Article Mapper is responsible for map all rows found in a sql 
     * statement to Article objects.
     */
    private static final class ArticleMapper implements RowMapper<Article> {

        @Override
        public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
            Article article = new Article();
            article.setId(rs.getInt("article_id"));
            article.setHeadline(rs.getString("headline"));

            return article;
        }

    }

}

测试上下文.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

    <context:component-scan base-package="dk.martinrohwedder.blog" />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="embeddedDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:blog"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <jdbc:embedded-database id="dataSource" type="HSQL"/>
        <jdbc:initialize-database data-source="embeddedDataSource">
        <jdbc:script location="classpath:create-db.sql"/>
        <jdbc:script location="classpath:test-data.sql"/>
    </jdbc:initialize-database>

</beans>

创建-db.sql

create database if not exists blog;

use blog;

drop table if exists article;

create table article
(
    article_id int unsigned not null auto_increment,
    headline varchar(30) not null,
    primary key (article_id)
);

测试数据.xml

insert into article (article_headline)
values ("Artikel 1");
insert into article (article_headline)
values ("Artikel 2");
insert into article (article_headline)
values ("Artikel 3");

test-context.xml、create-db.sql 和 test-data.sql 都在类路径中,因为它们位于名为 Other Sources (src/main/resources) 的包中。

希望任何人都可以帮助我。

4

2 回答 2

3

尝试 在 test-context.xml 文件中使用
<context:annotation-config/>
而不是。
<mvc:annotation-driven />

<mvc:annotation-driven/>是 Spring 3.0 中添加的一个标签,它执行以下操作:

  1. 配置 Spring 3 Type ConversionService(替代 PropertyEditors)
  2. 添加对使用 @NumberFormat 格式化数字字段的支持
  3. 如果 Joda Time 在类路径上,则添加对使用 @DateTimeFormat 格式化日期、日历和 Joda Time 字段的支持
  4. 如果 JSR-303 Provider 在类路径上,则添加对使用 @Valid 验证 @Controller 输入的支持
  5. 如果 JAXB 在类路径上,则添加对读取和写入 XML 的支持(使用 @RequestBody/@ResponseBody 的 HTTP 消息转换)
  6. 如果 Jackson 在类路径上,则添加对读取和写入 JSON 的支持(与 #5 相同)

<context:annotation-config/>
在定义的同一应用程序上下文中查找 bean 上的注释,并声明支持所有通用注释,如 @Autowired、@Resource、@Required、@PostConstruct 等。

于 2012-11-23T13:38:10.363 回答
1

在我看来,它articleDao没有被设置。如果您想在测试中使用 Spring bean,那么您不能在测试中实例化该服务。把这条线拿出来...

articleService = new ArticleService();
于 2012-11-23T10:49:14.783 回答