vendredi 10 avril 2015

Configure integration test or use Spring JDBC in Unit Test using Spring Boot?

Am using the latest versions of Spring Boot, Spring JDBC, and Spring Rest...


My project is setup as a typical Maven project containing the following filesystem structure:



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


My application.properties are as follows (connecting to a local MySQL 5 database):



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;
}
}


Its called in MyService class using Dependency Injection:



@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());
}
}


This totally works as stated in my RestController...


But however, in a typical JUnit test:



public class MyServiceTest {

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

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


The call to myDao.findAllApps() returns a NullPointerException...


I even tried running my app (using the embedded tomcat) first by issuing the following from the command line:



mvn spring-boot:run


However a non-database specific JUnit test works inside Eclipse or when I do:



mvn clean install


Question(s):




  1. How can I set it up so I can run an integration test and it actually hits my database (or a mock db for that matter) from MyServiceTest?




  2. Why is the dependency injection failing when trying to inject in MyServiceTest for Spring JDBC?




  3. Is there a way to setup my unit tests to test Rest calls?




Many thanks for everyone who took the time to read this and many many thanks for the people that respond!


Here's my pom.xml (per Eddu's request):



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://ift.tt/IH78KX"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/HBk9RF"
xmlns:xsi="http://ift.tt/ra1lAU">
<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>http://ift.tt/1A9iaEo;
</repository>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>http://ift.tt/NpWKvb;
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://ift.tt/1A9iaEo;
</pluginRepository>
</pluginRepositories>

</project>

Aucun commentaire:

Enregistrer un commentaire