@Configuration @Profile("db1") public class DBConfig { @Bean public javax.sql.DataSource.DataSource dataSource(){ org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); ds.setDriverClassName("dbDriver"); ds.setUrl("dbUrl"); ds.setUsername("dbUser"); ds.setPassword(""); ds.setInitialSize(5); ds.setMaxActive(10); ds.setMaxIdle(5); ds.setMinIdle(2); return ds; } }
@ContextConfiguration(classes = DBConfig.class) @ActiveProfiles("db1") public class DBUnitConfig extends DBTestCase{ @Autowired javax.sql.DataSource dataSource; protected IDatabaseTester tester; protected IDataSet beforeData; private TestContextManager testContextManager; @Before public void setUp() throws Exception { this.testContextManager = new TestContextManager(getClass()); this.testContextManager.prepareTestInstance(this); tester = new DataSourceDatabaseTester(dataSource); } @Override protected IDataSet getDataSet() throws Exception { return beforeData; } @Override protected DatabaseOperation getTearDownOperation() throws Exception { return DatabaseOperation.NONE; } }
// this.testContextManager = new TestContextManager(getClass()); this.testContextManager.prepareTestInstance(this);
// RunWith , @RunWith(ParallelRunner.class) // : , , // ( ) @PerfTest(duration = 10000, threads = 50, timer = RandomTimer.class, timerParams = { 5, 15 }) // : , @Required(max = 500, average = 100) public class PerfTest extends DBUnitConfig { // ContiPerf @Rule public ContiPerfRule i = new ContiPerfRule(); // @Before public void setUp() throws Exception { super.setUp(); // xml : "<dataset></dataset>" beforeData = new FlatXmlDataSetBuilder().build( Thread.currentThread().getContextClassLoader() .getResourceAsStream("initData.xml")); tester.setDataSet(beforeData); tester.onSetup(); } // @Test public void test1() { Connection sqlConnection = null; CallableStatement statement = null; ResultSet set = null; try { sqlConnection = tester.getConnection().getConnection(); sqlConnection.setAutoCommit(true); statement = sqlConnection.prepareCall("select smth from tbl where param=?"); statement.setString(1, "prm"); set = statement.executeQuery(); set.next(); int smth1 = set.getInt(1); statement.close(); statement = sqlConnection.prepareCall("{ ? = call pkg.function(?) }"); statement.registerOutParameter(1, (Types.INTEGER)); statement.setString(2, "prm"); statement.execute(); int smth2 = statement.getInt(1); statement.close(); } catch(Exception ex){ System.out.print(ex.getMessage()); } finally { try { set.close(); statement.close(); sqlConnection.close();} catch (Exception e){ System.out.print(e.getMessage());} } Assert.assertEquals(smth1, smth2); } }
Source: https://habr.com/ru/post/228719/
All Articles