[SPR-6184] added tests to verify support for @Configuration classes with TestNG; simplified existing TestNG tests using Spring 3.0 and 3.1 features.

This commit is contained in:
Sam Brannen 2011-04-09 22:44:53 +00:00
parent 522a879496
commit 174bf58308
9 changed files with 273 additions and 103 deletions

View File

@ -0,0 +1,154 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.testng;
import static org.junit.Assert.assertNotNull;
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
import static org.springframework.test.transaction.TransactionTestUtils.inTransaction;
import static org.testng.Assert.assertEquals;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* Integration tests that verify support for
* {@link import org.springframework.context.annotation.Configuration @Configuration}
* classes with TestNG-based tests.
*
* <p>Configuration will be loaded from
* {@link AnnotationConfigTransactionalTestNGSpringContextTestsConfig}.
*
* @author Sam Brannen
* @since 3.1
*/
@SuppressWarnings("deprecation")
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class AnnotationConfigTransactionalTestNGSpringContextTests extends
AbstractTransactionalTestNGSpringContextTests {
private static final String JANE = "jane";
private static final String SUE = "sue";
private static final String YODA = "yoda";
private static final int NUM_TESTS = 2;
private static final int NUM_TX_TESTS = 1;
private static int numSetUpCalls = 0;
private static int numSetUpCallsInTransaction = 0;
private static int numTearDownCalls = 0;
private static int numTearDownCallsInTransaction = 0;
@Autowired
private Employee employee;
@Autowired
private Pet pet;
private int createPerson(String name) {
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private int deletePerson(String name) {
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
+ testState + ").");
}
private void assertAddPerson(final String name) {
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
}
@BeforeClass
public void beforeClass() {
numSetUpCalls = 0;
numSetUpCallsInTransaction = 0;
numTearDownCalls = 0;
numTearDownCallsInTransaction = 0;
}
@AfterClass
public void afterClass() {
assertEquals(numSetUpCalls, NUM_TESTS, "number of calls to setUp().");
assertEquals(numSetUpCallsInTransaction, NUM_TX_TESTS, "number of calls to setUp() within a transaction.");
assertEquals(numTearDownCalls, NUM_TESTS, "number of calls to tearDown().");
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
}
@Test
@NotTransactional
public void autowiringFromConfigClass() {
assertNotNull("The employee should have been autowired.", employee);
assertEquals("John Smith", employee.getName());
assertNotNull("The pet should have been autowired.", pet);
assertEquals("Fido", pet.getName());
}
@BeforeTransaction
public void beforeTransaction() {
assertNumRowsInPersonTable(1, "before a transactional test method");
assertAddPerson(YODA);
}
@BeforeMethod
public void setUp() throws Exception {
numSetUpCalls++;
if (inTransaction()) {
numSetUpCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertAddPerson(JANE);
assertAddPerson(SUE);
assertNumRowsInPersonTable(4, "in modifyTestDataWithinTransaction()");
}
@AfterMethod
public void tearDown() throws Exception {
numTearDownCalls++;
if (inTransaction()) {
numTearDownCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
}
@AfterTransaction
public void afterTransaction() {
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
assertNumRowsInPersonTable(1, "after a transactional test method");
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.testng;
import javax.sql.DataSource;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.transaction.PlatformTransactionManager;
/**
* ApplicationContext configuration for {@link AnnotationConfigTransactionalTestNGSpringContextTests}.
*
* @author Sam Brannen
* @since 3.1
*/
@Configuration
public class AnnotationConfigTransactionalTestNGSpringContextTestsConfig {
@Bean
public Employee employee() {
Employee employee = new Employee();
employee.setName("John Smith");
employee.setAge(42);
employee.setCompany("Acme Widgets, Inc.");
return employee;
}
@Bean
public Pet pet() {
return new Pet("Fido");
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
.addScript("classpath:/org/springframework/test/context/testng/schema.sql")//
.addScript("classpath:/org/springframework/test/context/testng/data.sql")//
.build();
}
}

View File

@ -1,33 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean id="employee" class="org.springframework.beans.Employee">
<property name="name" value="John Smith" />
<property name="age" value="42" />
<property name="company" value="Acme Widgets, Inc." />
</bean>
<bean id="employee" class="org.springframework.beans.Employee" p:name="John Smith" p:age="42"
p:company="Acme Widgets, Inc." />
<bean id="pet" class="org.springframework.beans.Pet">
<constructor-arg value="Fido" />
</bean>
<bean id="pet" class="org.springframework.beans.Pet" c:_="Fido" />
<bean id="foo" class="java.lang.String">
<constructor-arg value="Foo" />
</bean>
<bean id="foo" class="java.lang.String" c:_="Foo" />
<bean id="bar" class="java.lang.String">
<constructor-arg value="Bar" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
<bean id="bar" class="java.lang.String" c:_="Bar" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />
<bean id="databaseSetup"
class="org.springframework.test.context.testng.ConcreteTransactionalTestNGSpringContextTests$DatabaseSetup" />
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:/org/springframework/test/context/testng/schema.sql" />
<jdbc:script location="classpath:/org/springframework/test/context/testng/data.sql" />
</jdbc:embedded-database>
</beans>

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,20 +24,16 @@ import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
@ -56,18 +52,18 @@ import org.testng.annotations.Test;
public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTransactionalTestNGSpringContextTests
implements BeanNameAware, InitializingBean {
private static final String BOB = "bob";
private static final String JANE = "jane";
private static final String SUE = "sue";
private static final String YODA = "yoda";
private static final int NUM_TESTS = 8;
private static final int NUM_TX_TESTS = 1;
private static int numSetUpCalls = 0;
private static int numSetUpCallsInTransaction = 0;
private static int numTearDownCalls = 0;
private static int numTearDownCallsInTransaction = 0;
// ------------------------------------------------------------------------|
private boolean beanInitialized = false;
private String beanName = "replace me with [" + getClass().getName() + "]";
@ -86,35 +82,14 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
protected String bar;
// ------------------------------------------------------------------------|
private static int clearPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
return SimpleJdbcTestUtils.deleteFromTables(simpleJdbcTemplate, "person");
}
private static void createPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
try {
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
}
catch (BadSqlGrammarException bsge) {
/* ignore */
}
}
private static int countRowsInPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "person");
}
private static int addPerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
private int createPerson(String name) {
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private static int deletePerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
private int deletePerson(String name) {
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
// ------------------------------------------------------------------------|
public void afterPropertiesSet() throws Exception {
this.beanInitialized = true;
}
@ -133,19 +108,15 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
this.bar = bar;
}
// ------------------------------------------------------------------------|
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
assertEquals(countRowsInPersonTable(this.simpleJdbcTemplate), expectedNumRows,
"Verifying the number of rows in the person table (" + testState + ").");
assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
+ testState + ").");
}
private void assertAddPerson(final String name) {
assertEquals(addPerson(this.simpleJdbcTemplate, name), 1, "Adding '" + name + "'");
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
}
// ------------------------------------------------------------------------|
@BeforeClass
public void beforeClass() {
numSetUpCalls = 0;
@ -156,10 +127,10 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@AfterClass
public void afterClass() {
assertEquals(numSetUpCalls, 8, "Verifying number of calls to setUp().");
assertEquals(numSetUpCallsInTransaction, 1, "Verifying number of calls to setUp() within a transaction.");
assertEquals(numTearDownCalls, 8, "Verifying number of calls to tearDown().");
assertEquals(numTearDownCallsInTransaction, 1, "Verifying number of calls to tearDown() within a transaction.");
assertEquals(numSetUpCalls, NUM_TESTS, "number of calls to setUp().");
assertEquals(numSetUpCallsInTransaction, NUM_TX_TESTS, "number of calls to setUp() within a transaction.");
assertEquals(numTearDownCalls, NUM_TESTS, "number of calls to tearDown().");
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
}
@Test
@ -169,14 +140,14 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
assertNotNull(super.applicationContext,
"The application context should have been set due to ApplicationContextAware semantics.");
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
assertEquals(employeeBean.getName(), "John Smith", "Verifying employee's name.");
assertEquals(employeeBean.getName(), "John Smith", "employee's name.");
}
@Test
@NotTransactional
public void verifyBeanInitialized() {
assertInTransaction(false);
assertTrue(this.beanInitialized,
assertTrue(beanInitialized,
"This test instance should have been initialized due to InitializingBean semantics.");
}
@ -184,7 +155,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@NotTransactional
public void verifyBeanNameSet() {
assertInTransaction(false);
assertEquals(this.beanName, getClass().getName(),
assertEquals(beanName, getClass().getName(),
"The bean name of this test instance should have been set due to BeanNameAware semantics.");
}
@ -192,35 +163,33 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@NotTransactional
public void verifyAnnotationAutowiredFields() {
assertInTransaction(false);
assertNull(this.nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
assertNotNull(this.pet, "The pet field should have been autowired.");
assertEquals(this.pet.getName(), "Fido", "Verifying pet's name.");
assertNull(nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
assertNotNull(pet, "The pet field should have been autowired.");
assertEquals(pet.getName(), "Fido", "pet's name.");
}
@Test
@NotTransactional
public void verifyAnnotationAutowiredMethods() {
assertInTransaction(false);
assertNotNull(this.employee, "The setEmployee() method should have been autowired.");
assertEquals(this.employee.getName(), "John Smith", "Verifying employee's name.");
assertNotNull(employee, "The setEmployee() method should have been autowired.");
assertEquals(employee.getName(), "John Smith", "employee's name.");
}
@Test
@NotTransactional
public void verifyResourceAnnotationInjectedFields() {
assertInTransaction(false);
assertEquals(this.foo, "Foo", "The foo field should have been injected via @Resource.");
assertEquals(foo, "Foo", "The foo field should have been injected via @Resource.");
}
@Test
@NotTransactional
public void verifyResourceAnnotationInjectedMethods() {
assertInTransaction(false);
assertEquals(this.bar, "Bar", "The setBar() method should have been injected via @Resource.");
assertEquals(bar, "Bar", "The setBar() method should have been injected via @Resource.");
}
// ------------------------------------------------------------------------|
@BeforeTransaction
public void beforeTransaction() {
assertNumRowsInPersonTable(1, "before a transactional test method");
@ -255,22 +224,8 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
@AfterTransaction
public void afterTransaction() {
assertEquals(deletePerson(this.simpleJdbcTemplate, YODA), 1, "Deleting yoda");
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
assertNumRowsInPersonTable(1, "after a transactional test method");
}
// ------------------------------------------------------------------------|
public static class DatabaseSetup {
@Autowired
void setDataSource(DataSource dataSource) {
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
createPersonTable(simpleJdbcTemplate);
clearPersonTable(simpleJdbcTemplate);
addPerson(simpleJdbcTemplate, BOB);
}
}
}

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
<jdbc:embedded-database id="dataSource" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
<jdbc:embedded-database id="dataSource" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />

View File

@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa"
p:password="" />
<jdbc:embedded-database id="dataSource" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:data-source-ref="dataSource" />

View File

@ -0,0 +1 @@
INSERT INTO person VALUES('bob');

View File

@ -0,0 +1,4 @@
CREATE TABLE person (
name VARCHAR(20) NOT NULL,
PRIMARY KEY(name)
);