使用Spring Boot配置集成测试或在单元测试中使用Spring JDBC?

时间:2015-04-09 04:41:35

标签: spring junit spring-boot integration-testing spring-jdbc

使用最新版本的Spring Boot,Spring JDBC和Spring Rest ......

我的项目设置为包含以下文件系统结构的典型Maven项目:

myproject
|
--src/main/java/com/myapp
--src/main/resource/application.properties
|
--src/test/java/com/myapp
--src/test/resources/application.properties
|
pom.xml

我的application.properties如下(连接到本地MySQL 5数据库):

spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.name=testdb
spring.datasource.initialize=true
spring.datasource.driverClassName=com.mysql.jdbc.Driver      

MyDAO:

public interface MyDao {

    public List<App> findAllApps(); 
}

MyDaoImpl:

@Repository("myDao")
public class MyDaoImpl implements MyDao {

    @Autowired 
    JdbcTemplate jdbcTemplate;

    public List<App> findAllApps() {
        List<App> apps = this.jdbcTemplate.query(
                "select app_name from app",
                new RowMapper<App>() {
                    public App mapRow(ResultSet rs, int rowNum) throws SQLException {
                        App app = new App();
                        app.setAppName(rs.getString("app_name"));
                        return app;
                    }
                });

        return apps;
    }
}

使用依赖注入在MyService类中调用它:

@RestController
public class MyService {

    @Autowired
    @Qualifier("myDao")
    MyDao myDao;

    @RequestMapping(value = "/apps", method = RequestMethod.GET, consumes = "text/plain", produces = "application/json")
    public void process() throws JsonParseException, IOException {
        List<App> apps = myDao.findAllApps();
        System.out.println(apps.toString());
    }
}

这完全适用于我的RestController ......

但是,在典型的JUnit测试中:

public class MyServiceTest {

    @Autowired
    @Qualifier("myDao")
    MyDao myDao;

    @Test
    public void process() {
        List<App> apps = myDao.findAllApps();
    }
}

对myDao.findAllApps()的调用返回NullPointerException ...

我甚至尝试通过从命令行发出以下命令来运行我的应用程序(使用嵌入式tomcat):

mvn spring-boot:run 

然而,非数据库特定的JUnit测试在Eclipse内部或我这样做时起作用:

mvn clean install

问题(S):

  1. 如何进行设置以便我可以运行集成测试,它实际上是从MyServiceTest点击我的数据库(或模拟数据库)?

  2. 为什么在尝试为Spring JDBC注入MyServiceTest时依赖注入失败?

  3. 有没有办法设置我的单元测试来测试Rest调用?

  4. 非常感谢所有花时间阅读本文的人,感谢那些回应的人!

    这是我的pom.xml(根据Eddu的要求):

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.springframework</groupId>
        <artifactId>myproject</artifactId>
        <version>0.1.0</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.2.RELEASE</version>
        </parent>
    
        <properties>
            <java.version>1.7</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-rest-webmvc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.0</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-releases</id>
                <name>Spring Releases</name>
                <url>https://repo.spring.io/libs-release</url>
            </repository>
            <repository>
                <id>org.jboss.repository.releases</id>
                <name>JBoss Maven Release Repository</name>
                <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
            </repository>
        </repositories>
    
        <pluginRepositories>
            <pluginRepository>
                <id>spring-releases</id>
                <name>Spring Releases</name>
                <url>https://repo.spring.io/libs-release</url>
            </pluginRepository>
        </pluginRepositories>
    
    </project>
    

2 个答案:

答案 0 :(得分:3)

您的测试中似乎没有加载Spring的上下文,因此不会执行依赖注入。你应该做点什么:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Application.class) //Application being your
// Spring boot base config class
public class MyServiceTest { ... }

    @Autowired
    @Qualifier("myDao")
    MyDao myDao;

您可以查看我的github

中的示例

答案 1 :(得分:1)

谢谢大家!

史蒂夫 - 你是对的!

经过多次试验&amp;苦难,@ SpringApplicationConfiguration工作:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=Application.class)

Eddu Melendez - 我使用spring-boot-starter-jdbc删除了spring-starter-data-jpa依赖项,并从我的pom.xml文件中删除了spring-jdbc依赖项。

谢谢大家帮我解决这个问题!