我有下表(迁移):
databaseChangeLog:
- changeSet:
id: 1
author: me
changes:
- createTable:
tableName: person
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: first_name
type: varchar(255)
constraints:
nullable: false
- column:
name: last_name
type: varchar(255)
constraints:
nullable: false
- changeSet:
id: 2
author: me
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: First
- column:
name: last_name
value: Last
public interface HelloDao {
@SqlQuery(
"SELECT * FROM person"
)
@Mapper(PersonMapper.class)
Person getPerson();
}
使用以下实体+映射器:
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
-
public class PersonMapper implements ResultSetMapper<Person> {
public Person map(int index, ResultSet r, StatementContext ctx) throws SQLException {
return new Person(r.getString("first_name"), r.getString("last_name"));
}
}
我使用junit和unitils为这个DAO编写单元测试。我将unitils.properties
文件设置为:
database.driverClassName=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/springbootdb
database.dialect=postgresql
database.userName=<username>
database.password=<password>
database.schemaNames=public
为了清楚起见,下面是我的application.properties
文件:
spring.thymeleaf.cache=false
spring.main.show-banner=false
logging.level.jdbc=OFF
logging.level.jdbc.sqltiming=DEBUG
logging.level.jdbc.resultsettable=DEBUG
spring.datasource.url= jdbc:postgresql://localhost:5432/springbootdb
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.jpa.hibernate.ddl-auto=create-drop
下面是我的测试:
@Transactional(TransactionMode.ROLLBACK)
@RunWith(UnitilsBlockJUnit4ClassRunner.class)
public class HelloDaoTest {
@TestDataSource
DataSource dataSource;
private DBI dbi;
private HelloDao helloDao;
@Before
public void setUp() throws Exception {
dbi = new DBI(dataSource);
helloDao = dbi.onDemand(HelloDao.class);
}
@Test
@DataSet("HelloDaoTest.testSet.xml")
public void helloDao() throws Exception {
Person p = helloDao.getPerson();
assertEquals("First", p.getFirstName());
}
}
和我的fixture(hellodaoTest.testset.xml
)
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<person
first_name="First"
last_name="Last"
/>
</dataset>
和pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.experiment.springboot</groupId>
<artifactId>testspringbootproj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Boot Blank Project (from https://github.com/making/spring-boot-blank)</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.experiment.springboot.App</start-class>
<java.version>1.8</java.version>
<lombok.version>1.14.8</lombok.version>
<log4jdbc.log4j2.version>1.16</log4jdbc.log4j2.version>
<rest.assured.version>2.3.3</rest.assured.version>
<jdbi.version>2.78</jdbi.version>
</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest.assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>${log4jdbc.log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi</artifactId>
<version>${jdbi.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-dbunit</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>${spring-loaded.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我运行这个测试时,我会得到以下错误:
java.lang.NullPointerException
at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:820)
at org.unitils.database.transaction.impl.DefaultUnitilsTransactionManager.rollback(DefaultUnitilsTransactionManager.java:157)
at org.unitils.database.DatabaseModule.rollbackTransaction(DatabaseModule.java:425)
at org.unitils.database.DatabaseModule.endTransactionForTestMethod(DatabaseModule.java:390)
at org.unitils.database.DatabaseModule$DatabaseTestListener.afterTestTearDown(DatabaseModule.java:486)
at org.unitils.core.Unitils$UnitilsTestListener.afterTestTearDown(Unitils.java:315)
at org.unitils.core.junit.AfterTestTearDownStatement.evaluate(AfterTestTearDownStatement.java:48)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.unitils.core.junit.BeforeTestClassStatement.evaluate(BeforeTestClassStatement.java:41)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
看起来甚至没有到达测试代码。我做错了什么?
在pom.xml中确实有两个数据库实现,但没有指定作用域。Spring无法猜测哪一个是正确的,因此是一个例外。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
[...]
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
这很可能会进一步帮助您:为Spring中的测试目的在内存数据库中配置特定的
问题内容: 我希望在编写用于测试某些数据库条目的单元测试中获得一些建议。 如果找不到记录,则我正在测试的功能会将数据库作为种子。 我似乎不太了解如何进行len测试。我正在使用测试数据库,因此我可以在任何时候都对其进行核对,因此,如果我只需要在函数上强制使用一个空的数据库,就没有问题了。 该函数本身可以正常工作,我只想确保已覆盖该函数。 任何建议都很好。 谢谢! 问题答案: 确实取决于您,有很多方法
我的Android项目有几个模块(一个应用程序和多个库),当我尝试从命令行运行所有单元测试(RobolectRic3.8)时 我得到以下错误(不是当我从Android Studio运行相同的任务时) 每个模块上的build.gradle文件包含以下配置 application模块定义了两个在同一维度中运行的味道 知道吗?
在以前的一个项目中,我使用Spock测试框架对Java代码进行单元测试。我发现这非常有效,所以我尝试将Spock测试添加到我当前的项目中,该项目使用Maven作为构建工具(前一个项目使用Gradle)。虽然我可以让Maven编译我的Spock测试(使用),但我无法让Maven运行这些测试。 我做了一个简单的例子来演示我在两个文件中的问题: null 当我执行(或)时,我希望运行单个单元测试并失败。
我有一个Spring Data JPA存储库,只要不添加Spring Security性依赖项(spring-boot-starter-security)并在存储库上添加相应的方法授权注释,单元测试就可以正常工作。添加后,在运行单元测试时,我会得到一个AuthenticationCredentialsNotFound异常。 如何在单元测试中“验证”对存储库方法的调用?
问题内容: 我开始采用TDD开发态度,并为django应用程序编写单元测试。我知道固定装置,并且知道应该执行测试的方式,但是对于给定的测试,我确实需要在整个数据库上执行它,而我想处理的10百万以上行数据库的json固定装置,此外,此测试是“只读”的。 因此,问题是如何设置测试套件以在生产数据库上运行?我想这就像在某些测试的setUp方法中添加DATABASE_NAME设置一样容易。但是运行测试时,