Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7627101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:24:54+00:00 2026-05-31T05:24:54+00:00

I’m looking for a way to run some TestNG tests against multiple DataSource s.

  • 0

I’m looking for a way to run some TestNG tests against multiple DataSources. I’ve seen the DataProvider options for TestNG, but they don’t quite do what I want. I want to run some really simple tests against my MyBatis mappers, mainly to make sure I don’t have any simple syntax errors, typos, etc. Right now, I have a base test class like this:

public class MapperTestBaseH2 {
    protected SqlSessionManager sessionManager;

    @BeforeClass
    public void beforeClass() throws SQLException, LiquibaseUpdateException {
        JdbcDataSource h2DataSource = new JdbcDataSource();
        h2DataSource.setURL("jdbc:h2:mem:test-db;DB_CLOSE_DELAY=-1");

        sessionManager = MyBatisUtils.createSqlSessionManager(h2DataSource);
        LiquibaseUpdater.update(h2DataSource.getConnection(), LiquibaseUpdater.DEFAULT_CONTEXTS);
    }

    @BeforeMethod
    public void beforeMethod() {
        sessionManager.startManagedSession();
    }

    @AfterMethod
    public void afterMethod() {
        sessionManager.rollback();
    }
}

Then I create a test for each mapper:

public class SequenceMapperTest extends MapperTestBaseH2 {
    private SequenceMapper sequenceMapper;

    @BeforeClass
    @Override
    public void beforeClass() throws SQLException, LiquibaseUpdateException {
        super.beforeClass();
        sequenceMapper = super.sessionManager.getMapper(SequenceMapper.class);
    }

    @Test
    public void selectBidSequenceTest() {
        sequenceMapper.selectSequence(Sequences.BID_SEQ);
    }

    @Test
    public void updateBidSequenceTest() {
        sequenceMapper.updateSequence(sequenceMapper.selectSequence(Sequences.BID_SEQ));
    }
}

What I’d like to do is run each test class twice (or more), using a different SqlSessionManager each time. I can use TestNG’s DataProvider to run each test with multiple SqlSessionManagers, but then I have to start & rollback the session inside each test rather than in @BeforeMethod.

Is there an easy way to accomplish what I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:24:55+00:00Added an answer on May 31, 2026 at 5:24 am

    I think I’ve got this figured out after stumbling across this post. The trick is to use a combination of @Factory and @DataProvider. First I created an abstract base class that manages the provider and the transactions.

    public abstract class MyBatisTestBase {
        public abstract SqlSessionManager getSessionManager();
    
        @BeforeMethod
        public void beforeMethod() {
            getSessionManager().startManagedSession();
        }
    
        @AfterMethod
        public void afterMethod() {
            getSessionManager().rollback();
        }
    
        @DataProvider(name = "sql-session-manager-provider")
        public static Object[][] sqlSessionManagerProvider() throws SQLException, LiquibaseUpdateException {
            return new Object[][] {
                    {createDerbySessionManager()},
                    {createH2SessionManager()}
            };
        }
    
        private static SqlSessionManager createDerbySessionManager() throws SQLException, LiquibaseUpdateException {
            EmbeddedDataSource40 dataSource = new EmbeddedDataSource40();
            dataSource.setDatabaseName("memory:test-db");
            dataSource.setCreateDatabase("create");
    
            LiquibaseUtils.update(dataSource.getConnection(), LiquibaseUtils.DEFAULT_CONTEXTS);
            return MyBatisUtils.createSqlSessionManager(dataSource);
        }
    
        private static SqlSessionManager createH2SessionManager() throws SQLException, LiquibaseUpdateException {
            JdbcDataSource dataSource = new JdbcDataSource();
            dataSource.setURL("jdbc:h2:mem:test-db;DB_CLOSE_DELAY=-1");
    
            LiquibaseUtils.update(dataSource.getConnection(), LiquibaseUtils.DEFAULT_CONTEXTS);
            return MyBatisUtils.createSqlSessionManager(dataSource);
        }
    }
    

    Then I use the @Factory annotation on the constructor of extending classes. This has the effect of creating one instance of the test class per SqlSessionManager from my @DataProvider.

    public class SequenceMapperTest extends MyBatisTestBase {
        private final SqlSessionManager sessionManager;
        private final SequenceMapper sequenceMapper;
    
        @Factory(dataProvider = "sql-session-manager-provider")
        public SequenceMapperTest(SqlSessionManager sessionManager) {
            this.sessionManager = sessionManager;
            this.sequenceMapper = sessionManager.getMapper(SequenceMapper.class);
        }
    
        @Test
        public void selectBidSequenceTest() {
            sequenceMapper.selectSequence(Sequences.BID_SEQ);
        }
    
        @Test
        public void updateBidSequenceTest() {
            sequenceMapper.updateSequence(sequenceMapper.selectSequence(Sequences.BID_SEQ));
        }
    
        @Override
        public SqlSessionManager getSessionManager() {
            return this.sessionManager;
        }
    }
    

    When I run my tests, two instances of the SequenceMapperTest get created, one for each SqlSessionManager that I set up in the @DataProvider. So far, it seems to be working, but my IDE (IntelliJ IDEA) has a bit of trouble splitting the logging output from each individual test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.