Git Product home page Git Product logo

dbunit-guice-mybatis's Introduction

DbUnit/Guice/MyBatis integration for testing of guicified DAO classes

This library greatly simplifies writing integration tests for projects employing MyBatis ORM framework and Guice dependency injection framework. It also makes DbUnit compatible with JUnit 4.

Usage

Add Maven dependency to your pom.xml:

<dependency>
    <groupId>org.plukh</groupId>
    <artifactId>dbunit-guice-mybatis</artifactId>
    <version>1.0.2</version>
    <scope>test</scope>
</dependency>

Create your TestMyBatisGuiceModule (or extend an existing one), along the lines of:

  public class MyBatisDAOModule extends XMLMyBatisModule { 
     public MyBatisDAOModule() {
     }
 
     @Override
     protected void initialize() {
         //Set environment
         setEnvironmentId("test");
 
         //Bind DAO implementations
         bind(TopicDAO.class).to(MyBatisTopicDAOImpl.class);
     }
  }

Create your test class:

  • Extend DbUnitTest class.
  • Add @GuiceModules(<your module classes here>) - note that all modules have to have public no-args constructors.
  • Optionally, add @DataSets annotation (either on the class itself or on individual test methods); set override to true for method datasets to completely replace class datasets (by default, they're combined together). DbUnitTest expects standard XmlDataSet dataset files - see library tests for some examples.
  • See DbUnitTestTest for examples on how to assert database state after update; TestUtils contains helpful methods to assert correctness of ordered and unordered collections returned by your DAO methods.

Requesting static injection

If test class is annotated with @RequestStaticInjection, its static members will be injected by Guice when injector is being created. This should be used with care, as it breaks isolation between tests - but is sometimes unavoidable (if you'd like to migrate/clean your database with Flyway before each test class, for example).

Note

This specific implementation of DbUnitTest is targeted towards MySQL. To make it work with other databases, override DbUnitTest#setUpDatabaseConfig(), DbUnitTest#getSetUpOperation() and DbUnitTest#getTearDownOperation(), adding specific operations required by your database (like, you may want to implement ALTER TABLE ... DISABLE TRIGGER for disabling foreign key checks in PostgreSQL, as it doesn't support global check on/off flag).

dbunit-guice-mybatis's People

Contributors

vdenisov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.