0
DB 및 TestNG를 사용하여 몇 가지 기본 자동화 테스트를 수행하려고하는데 작동하지 않습니다. 첫 번째 실행은 예상대로 성공합니다. 두 번째 실행은 첫 번째 실행이 롤백되지 않았기 때문에 수행되지 않습니다. 예를 들어 여러 곳을 살펴 봤는데 올바른 것으로 보입니다. 아무도 내가 놓친 거지 알고TestNG가있는 테스트 DB가 롤백되지 않습니다.
<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');
오류 :
CREATE SCHEMA의 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
A는'Connection' 매우 의심 보이는 반환합니다'Bean' @ 방법. 너 뭐하니? –
테스트 메소드의 전후에 SQL 스크립트를 실행하려면 이상적으로 Spring의'@Sql' 주석을 조사해야합니다. –
@SamBrannen 코드의 이전 반복이이를 사용했기 때문에 연결을 반환합니다.이를 제거하고 어떤 일이 일어나는지 봅니다. '@ Sql' 문법도 살펴 보겠습니다. –