实现Guice下的单元测试中mybatis数据库操作的回滚.

在Spring中,提供了@transaction注解来在单元测试中,回滚数据库的操作,
这样保证了单元测试的方法,不会对真实的数据库进行更新.从而更好地保持了各个单元测试方法的独立性.
在Guice中,并没有提供相应的机制. 我们可以通过自定义的BlockJUnit4ClassRunner继承子类来实现类似的功能.
代码如下:

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.ibatis.session.SqlSessionManager;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

public class MyBatisTestRollbackRunner extends BlockJUnit4ClassRunner {
    private Injector injector;
    @Inject
    private SqlSessionManager sqlSessionManager;

    public MyBatisTestRollbackRunner(final Class clazz) throws InitializationError {
        super(clazz);

        MyBatisModuleFactory myBatisModuleFactory = new MyBatisModuleFactory(new ConfigurationFactory());
        injector = Guice.createInjector(myBatisModuleFactory.get(clazz));
        injector.injectMembers(this);
    }

    @Override
    protected Statement methodInvoker(final FrameworkMethod method, final Object instance) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    invokeMethodInNewSession(method, instance);
                } finally {
                    cleanupSession();
                }
            }
        };
    }

    private void invokeMethodInNewSession(final FrameworkMethod method, final Object instance) throws Throwable {
        sqlSessionManager.startManagedSession();
        method.invokeExplosively(instance);
    }

    private void cleanupSession() {
        try {
            sqlSessionManager.rollback(true);
        } finally {
            sqlSessionManager.close();
        }
    }

    @Override
    protected Object createTest() throws Exception {
        Object testClass = super.createTest();
        injector.injectMembers(testClass);
        return testClass;
    }
}

import com.google.inject.name.Names;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.runners.model.InitializationError;
import org.mybatis.guice.MyBatisModule;
import org.mybatis.guice.datasource.builtin.PooledDataSourceProvider;

import java.util.Arrays;

public class MyBatisModuleFactory {
    private final ConfigurationFactory configurationFactory;

    public MyBatisModuleFactory(final ConfigurationFactory configurationFactory) {
        this.configurationFactory = configurationFactory;
    }

    public MyBatisModule get(final Class clazz) {
        return new MyBatisModule() {
            @Override
            protected void initialize() {
                bindDataSourceProviderType(PooledDataSourceProvider.class);
                bindTransactionFactoryType(JdbcTransactionFactory.class);
                addMapperClasses();
            }

            private void addMapperClasses() {
                try {
                    addMapperClasses(Arrays.asList(getAnnotatedMapperClasses(clazz)));
                } catch (Throwable e) {
                    throw new UnsupportedOperationException("can't add mapper classes");
                }
            }
        };
    }

    private Class[] getAnnotatedMapperClasses(final Class clazz) throws InitializationError {
        MapperClasses annotation = clazz.getAnnotation(MapperClasses.class);
        if (annotation == null) {
            throw new InitializationError(String.format("class '%s' must have a MapperClasses annotation", clazz.getName()));
        }
        return annotation.value();
    }
}

总体的实现思路.

  • 在创建测试方法时,使用自定义的injector.这个通过重载createTest()实现.
  • 在自定义的SqlSessionManager上执行测试方法.
    • 开启Session.
    • 执行测试方法.
    • 回滚Session.
  • 另外,通过annotation的方式,将我们需要测试的Mapper类注册到injector中.这个在getAnnotatedMapperClasses方法中实现.

例如,我们需要测试一个�MyMapper的Mapper类.那么使用以下的代码进行测试.

@RunWith(MyBatisTestRollbackRunner.class)
@MapperClasses({MyMapper.class})
public class MyMapperTest {

    @Inject
    MyMapper connectorMapper;
    
    //对MyMapper的增删改查操作的测试方法
    .....
}

你可能感兴趣的:(实现Guice下的单元测试中mybatis数据库操作的回滚.)