Ich versuche, einige grundlegende automatisierte Tests mit einer DB und TestNG zu tun, und es funktioniert nicht. Der erste Lauf gelingt wie erwartet, der zweite nicht, weil der erste nie zurück gerollt ist. Ich habe mir einige Beispiele angesehen und es scheint richtig zu sein. Wer weiß, was ichTest DB mit TestNG Rollback nicht
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
Code bin fehlt:
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');
Fehler:
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
Die '@ Bean' Methode, die ein' Connection' sieht sehr verdächtig zurückgibt. Wofür machst du das? –
Zum Ausführen eines SQL-Skripts vor oder nach einer Testmethode sollten Sie idealerweise Spring ''Sql'-Annotation betrachten. –
@SamBrannen Ich gebe diese Verbindung zurück, weil eine vorherige Iteration des Codes sie verwendet hat - ich werde sie entfernen und sehen, was passiert. Ich werde auch auf die @ Sql-Syntax –