JavaEar 专注于收集分享传播有价值的技术资料

What to use instead of SimpleJdbcTestUtils?

I am trying to set up a database test using Spring and DBUnit. I have a script to run before each test. The code below works but SimpleJdbcTemplate is deprecated. The method executeSqlScript is being moved to JdbcTestUtil as part of the 3.2 release but we're on 3.1.x. What to use instead?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTestScript{

    @Autowired
    public DataSource dataSource;
    @Autowired
    public Resource script;

    @Before
    public void setup() {
        SimpleJdbcTestUtils.executeSqlScript(
            new SimpleJdbcTemplate(dataSource), script, true);
    }

    @Test
    public void testInsert() {
    }
}

2个回答

    最佳答案
  1. Look into Liquibase http://www.liquibase.org/ its an excellent solution for doing migrations of the database, there is no reason why you can't use it from JUnit.

  2. 参考答案2
  3. You should just suppress the warning and go on. Once you switch to Spring 3.2, the transition will be trivial (remove all the Simple prefixes). There is no real reason to avoid SimpleJdbcTemplate at all costs in these tests, as its whole functionality is in JdbcTemplate under the same name.