我在我的后端使用Hibernate JPA.我正在使用JUnit和DBUnit编写单元测试,以将一组数据插入到内存中的HSQL数据库中.
我的数据集包含:
<order_line order_line_id="1" quantity="2" discount_price="0.3"/>
Run Code Online (Sandbox Code Playgroud)
它映射到OrderLine Java对象,其中discount_price列定义为:
@Column(name = "discount_price", precision = 12, scale = 2)
private BigDecimal discountPrice;
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的测试用例并声明返回的折扣价格等于0.3时,断言失败并表示存储的值为0.如果我将数据集中的discount_price更改为0.9,则它会向上舍入为1.
我已经检查过以确保HSQLDB没有进行舍入,这肯定不是因为我可以使用类似5.3的值的Java代码插入订单行对象并且它工作正常.
对我来说,似乎DBUtils由于某种原因舍入了我定义的数字.有没有办法可以迫使这种情况发生?任何人都可以解释为什么它可能这样做?
谢谢!
有哪些工具可用于填充mongodb中的测试数据.我们过去曾使用过dbunit,但它似乎没有相应的maven插件.
以下是我的pom.xml档案:
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/test</url>
<username>usernamet</username>
<password>password</password>
<dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
<metadataHandlerName>org.dbunit.ext.mysql.MySqlMetadataHandler</metadataHandlerName>
<encoding>utf-8</encoding>
<src>target/dbunit/export.xml</src><!--compare ? operation ???? -->
<type>CLEAN_INSERT</type><!--operation ????-->
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
</execution>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>operation</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
我dbunit:operation在命令行上运行mvn .
Scanning …Run Code Online (Sandbox Code Playgroud) 简而言之
我的命令行Java应用程序将数据从一个数据源复制到另一个数据源而不使用XA.我已经配置了两个单独的数据源,并希望能够回滚两个数据源上的数据的JUnit测试.我使用DBUnit将数据加载到"源"数据库中,但我不能让它回滚.我可以让"目标"数据源回滚.
我的守则
鉴于此配置......
<tx:annotation-driven />
<!-- note the default transactionManager name on this one -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceA" />
</bean>
<bean id="transactionManagerTarget" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceB" />
</bean>
Run Code Online (Sandbox Code Playgroud)
这段代码......
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:resources/spring-context.xml",
"classpath:resources/spring-db.xml"})
@Transactional
@TransactionConfiguration(transactionManager = "transactionManagerTarget", defaultRollback = true)
public class MyIntegrationTest {
@Autowired
private MyService service;
@Autowired
@Qualifier("dataSourceA")
private DataSource dataSourceA;
private IDataSet loadedDataSet;
/**
* Required by DbUnit
*/
@Before
public void setUp() throws Exception {
SybaseInsertIdentityOperation.TRUNCATE_TABLE.execute(getConnection(), getDataSet());
SybaseInsertIdentityOperation.INSERT.execute(getConnection(), getDataSet());
}
/**
* Required …Run Code Online (Sandbox Code Playgroud) 是否有另一种方法或某种工具来处理多个并行功能分支中的数据库模式更改,而不是为开发中的每个功能分支创建单独的数据库?
即.在内存中基于某些配置或脚本启动数据库,并在运行测试之前使用DbUnit填充.
这个问题专门针对单元测试,但也适用于UAT.
考虑典型的DBUnit Spring Test(请参阅https://github.com/springtestdbunit/spring-test-dbunit):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/META-INF/spring/applicationContext-database.xml",
"classpath:spring-*.xml"
})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@DatabaseSetup("/dbunit/data.xml")
public class UnitTest {
@Autowired
private UnitUnderTest uut;
@Test
public void shouldInitDB() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我已经验证的是,并且预计,自动装配将在DatabaseSetup之前发生.这必须发生,因为DBUnit依赖于应用程序上下文来提供配置的数据源.
问题是UnitUnderTest bean有一个@PostConstruct,它从DB加载一些数据但是,由于自动装配发生在DBunit设置之前,因此在这个阶段数据将不可用.
关于如何以干净的方式解决这个问题的任何想法?
如果我使用以下设置运行dbunit并在集成测试中通过HTTP请求数据,我没有得到任何数据,因为数据库是空的.DBUnit将数据写入数据库,但是当我通过HTTP请求数据时它是空的.
这是我的设置:Spring Boot 1.1.7 with spring-boot-starter-web(不包括tomcat),spring-boot-starter-jetty,spring-boot-starter-data-jpa,spring-boot-starter-test,liquibase -core,dbunit 2.5.0,spring-test-dbunit 1.1.0
主要应用类别:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@RestController
@EnableTransactionManagement
@EnableJpaRepositories
Run Code Online (Sandbox Code Playgroud)
测试配置(application-test.yaml):
logging.level.org.springframework: DEBUG
logging.level.org.dbunit: DEBUG
spring.jpa.properties.hibernate.hbm2ddl.auto: update
spring.jpa.database: h2
spring.jpa.show-sql: true
// setting these properties to access the database via h2 console
spring.datasource.url: jdbc:h2:tcp://localhost/mem:my_db;DB_CLOSE_DELAY=-1;MVCC=true;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username: sa
spring.datasource.password: sa
spring.datasource.driverClassName: org.h2.Driver
spring.jpa.database-platform: org.hibernate.dialect.H2Dialect
liquibase.change-log: classpath:/db/changelog/db-master.xml
Run Code Online (Sandbox Code Playgroud)
整合测试:
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HDImageService.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@WebAppConfiguration
@IntegrationTest("server.port:0")
@DatabaseSetup("/database_seed.xml")
@DatabaseTearDown(value = "/database_tear_down.xml", type = DatabaseOperation.DELETE_ALL)
// test
@Test
public void get_works() throws Exception {
// given …Run Code Online (Sandbox Code Playgroud) 最近我一直在使用JAXB / MOXy,它在我的所有测试和示例代码中都非常有用。我只使用绑定文件,所以才使用MOXy。
请注意,在我的所有示例中,我从未使用过ObjectFactory或jaxb.index,并且它的工作原理是GREAT。
回到我的业务时,我收到一个讨厌的JAXB异常,说我的包中没有ObjectFactory或jaxb.index。
我的项目还涉及Spring和Hibernate,JUnit和DBUnit。
这是一些示例代码:我有一个称为AContributionPhysicalSupport的抽象类。
package org.pea.openVillages.pojo.contribution.implementation;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "TOV_CONTRIBUTION_PHYSICAL_SUPPORT")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "SUPPORT_TYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
public abstract class AContributionPhysicalSupport implements Serializable
{
/* *****************************************************************
*
* PROPERTIES
*
* *****************************************************************
*/
/**
* for Serializable
*/
private static final long serialVersionUID = 1L;
@Id …Run Code Online (Sandbox Code Playgroud) 我运行测试时遇到以下错误:
org.dbunit.dataset.NoSuchColumnException: myTable.MYFIELD - (Non-uppercase input column: myfield) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.
at org.dbunit.dataset.AbstractTableMetaData.getColumnIndex(AbstractTableMetaData.java:117)
Run Code Online (Sandbox Code Playgroud)
我设置了断点org.dbunit.dataset.AbstractTableMetaData#getColumnIndex并发现了以下内容.在IntelliJ Idea中,该方法如下所示:
public int getColumnIndex(String columnName) throws DataSetException
{
logger.debug("getColumnIndex(columnName={}) - start", columnName);
if(this._columnsToIndexes == null)
{
// lazily create the map
this._columnsToIndexes = createColumnIndexesMap(this.getColumns());
}
String columnNameUpperCase = columnName.toUpperCase();
Integer colIndex = (Integer) this._columnsToIndexes.get(columnNameUpperCase);
if(colIndex != null)
{
return colIndex.intValue();
}
else
{
throw new NoSuchColumnException(this.getTableName(), columnNameUpperCase,
" (Non-uppercase input column: "+columnName+") in …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Junit4 迁移到 Junit5,因为我知道我正在使用 Spring Mvc 版本 4.3.5,并且我想知道是否可以通过某种方式使用 dbUnit 来处理我的类存储库
\n我做了研究,Dbunit 的最后一个依赖版本是 2.7.3 使用 Junit4。因此,我需要确认在使用 Junit 5 时无法使用 Dbunit,除非我决定迁移到 Spring 5 或 Spring Boot,因为我发现了一些讨论此问题的文章
\n我的旧版本班级测试的标题通常不需要它来回答我的问题,但由于质量原因我无法在这里发布问题,它是:
\nContextConfiguration\n\n@TestPropertySource(locations = "classpath:environnements.properties")\n@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,\n TransactionDbUnitTestExecutionListener.class })\n//TODO ajouter les xml des d\xc3\xa9pendances\n// faire par groupes de dependances dans chaque test ?\n@DatabaseSetup(value = {"/dbunit/RepositoryTyp-setup.xml", "/dbunit/RepositoryPTest-setup.xml"}, type = DatabaseOperation.CLEAN_INSERT)\npublic class PRepositoryTest extends SpringDbBasedTest {\nRun Code Online (Sandbox Code Playgroud)\n dbunit ×10
java ×5
spring ×4
hibernate ×3
junit ×3
testing ×2
database ×1
hsqldb ×1
jaxb ×1
junit5 ×1
maven ×1
mongodb ×1
moxy ×1
spring-boot ×1
spring-data ×1
spring-test ×1
unit-testing ×1