TestDataSet

Library to load test data to DB

License

License

Categories

Categories

Data
GroupId

GroupId

com.jpragma
ArtifactId

ArtifactId

testdataset
Last Version

Last Version

0.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

TestDataSet
Library to load test data to DB
Project URL

Project URL

https://github.com/jpragma/testdataset
Source Code Management

Source Code Management

https://github.com/jpragma/testdataset

Download testdataset

How to add to project

<!-- https://jarcasting.com/artifacts/com.jpragma/testdataset/ -->
<dependency>
    <groupId>com.jpragma</groupId>
    <artifactId>testdataset</artifactId>
    <version>0.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.jpragma/testdataset/
implementation 'com.jpragma:testdataset:0.1.2'
// https://jarcasting.com/artifacts/com.jpragma/testdataset/
implementation ("com.jpragma:testdataset:0.1.2")
'com.jpragma:testdataset:jar:0.1.2'
<dependency org="com.jpragma" name="testdataset" rev="0.1.2">
  <artifact name="testdataset" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jpragma', module='testdataset', version='0.1.2')
)
libraryDependencies += "com.jpragma" % "testdataset" % "0.1.2"
[com.jpragma/testdataset "0.1.2"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

TestDataSet is a simple java library that helps to populate tables in your relational database when writing unit/integration tests.

Quick start

  1. Create TestDataSetBuilder passing provider of java.sql.Connection to its constructor
  2. Use DSL to define tables, columns, and rows to insert (see example below)
  3. Call build() which returns an instance of TestDataSet
  4. In your unit test call TestDataSet.load() to populate the database with test data

Notes

  • You are responsible for creating transactions and rolling them back at the end of the test.
connection.setAutoCommit(false);
// ... 
connection.rollback();
  • When using Spring framework you can simply create a connection using org.springframework.jdbc.datasource.DataSourceUtils and transactions will automatically be started and rolled back at the end of the test.

Example

import com.jpragma.testdatasest.TestDataSet;
import com.jpragma.testdatasest.TestDataSetBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.time.LocalDate;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(SpringExtension.class)
@Transactional
@ContextConfiguration(classes = {PlayerConfig.class, TestDataSourceConfig.class})
class PlayerRepositoryTest {
    @Autowired
    private PlayerRepository repo;
    @Autowired
    private DataSource dataSource;

    private TestDataSet tds = new TestDataSetBuilder(() -> DataSourceUtils.getConnection(dataSource))
            .table("PLAYER")
                .columns("ID", "FIRST_NAME", "LAST_NAME", "JOINED_ON")
                .row(-1, "Isaac", "Levin", LocalDate.parse("2012-03-01"))
                .build();

    @Test
    void loadExistingPlayer() {
        tds.load();
        Optional<Player> player = repo.findById(-1);
        assertTrue(player.isPresent());
        assertEquals("Isaac", player.get().getFirstName());
    }
}

Versions

Version
0.1.2