【发布时间】:2017-10-30 18:48:41
【问题描述】:
我正在尝试使用 DB 和 TestNG 进行一些基本的自动化测试,但它不起作用。第一次运行成功,如我所料,第二次没有,因为第一次从未回滚。我已经查看了几个地方的示例,这似乎是正确的。任何人都知道我错过了什么
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
代码:
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;
@ContextConfiguration(classes = AutomatedTest.Context.class)
public class RollbackTest extends AbstractTransactionalTestNGSpringContextTests {
@Test
@Rollback
@Transactional
public void testThing() throws Exception {
Class<? extends RollbackTest> c = this.getClass();
String path = String.format("/%s.sql", c.getName().replaceAll("\\.", "/"));
super.executeSqlScript(path, false);
}
@Configuration
@PropertySource("db.properties")
static class Context {
@Bean
public DataSource dataSource(
@Value("${datasource.url}") String url,
@Value("${datasource.username}") String user,
@Value("${datasource.password}") String pass,
@Value("${datasource.driver-class-name}") String driver) throws PropertyVetoException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setUser(user);
ds.setPassword(pass);
ds.setJdbcUrl(url);
ds.setDriverClass(driver);
return ds;
}
@Bean
public Connection connection(DataSource dataSource) throws SQLException {
Connection c = dataSource.getConnection();
System.out.println("Connection is " + c);
return c;
}
@Bean
public DataSourceTransactionManager txMan(DataSource ds) {
return new DataSourceTransactionManager(ds);
}
}
}
sql:
CREATE SCHEMA FOO;
CREATE TABLE FOO.BAZ (
ID int PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(256) NOT NULL
);
INSERT INTO FOO.BAZ (name) values('christian');
错误:
创建架构 FOO
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [automated/RollbackTest.sql]: CREATE SCHEMA FOO; nested exception is java.sql.SQLException: Can't create database 'FOO'; database exists
【问题讨论】:
-
返回
Connection的@Bean方法看起来很可疑。你这样做是为了什么? -
要在测试方法之前或之后执行 SQL 脚本,最好查看 Spring 的
@Sql注释。 -
@SamBrannen 我正在返回该连接,因为之前的代码迭代正在使用它 - 我将删除它并看看会发生什么。我还要看看
@Sql语法 -
@SamBrannen 我按照你的建议做了:
@Sql注释完全符合我在代码中尝试做的事情,因此仅此一项就值得一些分数!我还删除了连接,它现在似乎工作了。你为什么不把你的建议变成一个答案,这样我就可以给你信用 -
很高兴它对你有用。
标签: java mysql spring testng spring-test