Do not proxy test instances based on "original instance" convention
Issue: SPR-17137
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -43,13 +43,6 @@ import static org.junit.Assert.*;
|
||||
@ContextConfiguration("context.groovy")
|
||||
public class GroovySpringContextTests implements BeanNameAware, InitializingBean {
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@Autowired
|
||||
@@ -63,41 +56,49 @@ public class GroovySpringContextTests implements BeanNameAware, InitializingBean
|
||||
|
||||
protected String bar;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
|
||||
@Autowired
|
||||
protected final void setEmployee(final Employee employee) {
|
||||
protected void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected final void setBar(final String bar) {
|
||||
protected void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setBeanName(final String beanName) {
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public final void verifyBeanInitialized() {
|
||||
public void verifyBeanNameSet() {
|
||||
assertTrue("The bean name of this test instance should have been set to the fully qualified class name " +
|
||||
"due to BeanNameAware semantics.", this.beanName.startsWith(getClass().getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyBeanInitialized() {
|
||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
|
||||
this.beanInitialized);
|
||||
this.beanInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyBeanNameSet() {
|
||||
assertEquals("The bean name of this test instance should have been set to the fully qualified class name "
|
||||
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAnnotationAutowiredFields() {
|
||||
public void verifyAnnotationAutowiredFields() {
|
||||
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong);
|
||||
assertNotNull("The application context should have been autowired.", this.applicationContext);
|
||||
assertNotNull("The pet field should have been autowired.", this.pet);
|
||||
@@ -105,18 +106,18 @@ public class GroovySpringContextTests implements BeanNameAware, InitializingBean
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAnnotationAutowiredMethods() {
|
||||
public void verifyAnnotationAutowiredMethods() {
|
||||
assertNotNull("The employee setter method should have been autowired.", this.employee);
|
||||
assertEquals("Dilbert", this.employee.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyResourceAnnotationWiredFields() {
|
||||
public void verifyResourceAnnotationWiredFields() {
|
||||
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyResourceAnnotationWiredMethods() {
|
||||
public void verifyResourceAnnotationWiredMethods() {
|
||||
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -51,10 +51,6 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
|
||||
private static final String SUE = "sue";
|
||||
private static final String YODA = "yoda";
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@Autowired
|
||||
@@ -68,30 +64,70 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
|
||||
|
||||
private String bar;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
|
||||
@Autowired
|
||||
private final void setEmployee(final Employee employee) {
|
||||
private void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
private final void setBar(final String bar) {
|
||||
private void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setBeanName(final String beanName) {
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
public void afterPropertiesSet() {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
assertEquals("Verifying the number of rows in the person table before a test method.",
|
||||
(inTransaction() ? 2 : 1), countRowsInPersonTable());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
assertEquals("Verifying the number of rows in the person table after a test method.",
|
||||
(inTransaction() ? 4 : 1), countRowsInPersonTable());
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
assertEquals("Verifying the number of rows in the person table before a transactional test method.",
|
||||
1, countRowsInPersonTable());
|
||||
assertEquals("Adding yoda", 1, addPerson(YODA));
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
assertEquals("Deleting yoda", 1, deletePerson(YODA));
|
||||
assertEquals("Verifying the number of rows in the person table after a transactional test method.",
|
||||
1, countRowsInPersonTable());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void verifyBeanNameSet() {
|
||||
assertInTransaction(false);
|
||||
assertTrue("The bean name of this test instance should have been set to the fully qualified class name " +
|
||||
"due to BeanNameAware semantics.", this.beanName.startsWith(getClass().getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyApplicationContext() {
|
||||
public void verifyApplicationContext() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
super.applicationContext);
|
||||
@@ -99,7 +135,7 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyBeanInitialized() {
|
||||
public void verifyBeanInitialized() {
|
||||
assertInTransaction(false);
|
||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
|
||||
this.beanInitialized);
|
||||
@@ -107,15 +143,7 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyBeanNameSet() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("The bean name of this test instance should have been set to the fully qualified class name "
|
||||
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyAnnotationAutowiredFields() {
|
||||
public void verifyAnnotationAutowiredFields() {
|
||||
assertInTransaction(false);
|
||||
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong);
|
||||
assertNotNull("The pet field should have been autowired.", this.pet);
|
||||
@@ -124,7 +152,7 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyAnnotationAutowiredMethods() {
|
||||
public void verifyAnnotationAutowiredMethods() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull("The employee setter method should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
@@ -132,58 +160,33 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyResourceAnnotationWiredFields() {
|
||||
public void verifyResourceAnnotationWiredFields() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public final void verifyResourceAnnotationWiredMethods() {
|
||||
public void verifyResourceAnnotationWiredMethods() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1,
|
||||
countRowsInPersonTable());
|
||||
assertEquals("Adding yoda", 1, addPerson(YODA));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
assertEquals("Verifying the number of rows in the person table before a test method.",
|
||||
(inTransaction() ? 2 : 1), countRowsInPersonTable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Adding jane", 1, addPerson(JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(SUE));
|
||||
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", 4,
|
||||
countRowsInPersonTable());
|
||||
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().",
|
||||
4, countRowsInPersonTable());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
assertEquals("Verifying the number of rows in the person table after a test method.",
|
||||
(inTransaction() ? 4 : 1), countRowsInPersonTable());
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
assertEquals("Deleting yoda", 1, deletePerson(YODA));
|
||||
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1,
|
||||
countRowsInPersonTable());
|
||||
}
|
||||
|
||||
private int addPerson(final String name) {
|
||||
private int addPerson(String name) {
|
||||
return super.jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
}
|
||||
|
||||
private int deletePerson(final String name) {
|
||||
private int deletePerson(String name) {
|
||||
return super.jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import javax.inject.Named;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -82,13 +81,9 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||
* Default resource path for the application context configuration for
|
||||
* {@link SpringJUnit4ClassRunnerAppCtxTests}: {@value #DEFAULT_CONTEXT_RESOURCE_PATH}
|
||||
*/
|
||||
public static final String DEFAULT_CONTEXT_RESOURCE_PATH = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
|
||||
public static final String DEFAULT_CONTEXT_RESOURCE_PATH =
|
||||
"/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@@ -124,31 +119,20 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||
@Named("quux")
|
||||
protected String namedQuux;
|
||||
|
||||
private String beanName;
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
@Override
|
||||
public final void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setBeanName(final String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected final void setEmployee(final Employee employee) {
|
||||
protected void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected final void setBar(final String bar) {
|
||||
protected void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
@@ -162,33 +146,46 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||
this.spelParameterValue = spelParameterValue;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
@Override
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public final void verifyApplicationContextSet() {
|
||||
public void verifyBeanNameSet() {
|
||||
assertTrue("The bean name of this test instance should have been set due to BeanNameAware semantics.",
|
||||
this.beanName.startsWith(getClass().getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyApplicationContextSet() {
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
this.applicationContext);
|
||||
this.applicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyBeanInitialized() {
|
||||
public void verifyBeanInitialized() {
|
||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
|
||||
this.beanInitialized);
|
||||
this.beanInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyBeanNameSet() {
|
||||
assertEquals("The bean name of this test instance should have been set due to BeanNameAware semantics.",
|
||||
getClass().getName(), this.beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAnnotationAutowiredAndInjectedFields() {
|
||||
public void verifyAnnotationAutowiredAndInjectedFields() {
|
||||
assertNull("The nonrequiredLong field should NOT have been autowired.", this.nonrequiredLong);
|
||||
assertEquals("The quux field should have been autowired via @Autowired and @Qualifier.", "Quux", this.quux);
|
||||
assertEquals("The namedFoo field should have been injected via @Inject and @Named.", "Quux", this.namedQuux);
|
||||
assertSame("@Autowired/@Qualifier and @Inject/@Named quux should be the same object.", this.quux,
|
||||
this.namedQuux);
|
||||
assertSame("@Autowired/@Qualifier and @Inject/@Named quux should be the same object.", this.quux, this.namedQuux);
|
||||
|
||||
assertNotNull("The pet field should have been autowired.", this.autowiredPet);
|
||||
assertNotNull("The pet field should have been injected.", this.injectedPet);
|
||||
@@ -198,13 +195,13 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAnnotationAutowiredMethods() {
|
||||
public void verifyAnnotationAutowiredMethods() {
|
||||
assertNotNull("The employee setter method should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAutowiredAtValueFields() {
|
||||
public void verifyAutowiredAtValueFields() {
|
||||
assertNotNull("Literal @Value field should have been autowired", this.literalFieldValue);
|
||||
assertNotNull("SpEL @Value field should have been autowired.", this.spelFieldValue);
|
||||
assertEquals("enigma", this.literalFieldValue);
|
||||
@@ -212,7 +209,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAutowiredAtValueMethods() {
|
||||
public void verifyAutowiredAtValueMethods() {
|
||||
assertNotNull("Literal @Value method parameter should have been autowired.", this.literalParameterValue);
|
||||
assertNotNull("SpEL @Value method parameter should have been autowired.", this.spelParameterValue);
|
||||
assertEquals("enigma", this.literalParameterValue);
|
||||
@@ -220,12 +217,12 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyResourceAnnotationInjectedFields() {
|
||||
public void verifyResourceAnnotationInjectedFields() {
|
||||
assertEquals("The foo field should have been injected via @Resource.", "Foo", this.foo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyResourceAnnotationInjectedMethods() {
|
||||
public void verifyResourceAnnotationInjectedMethods() {
|
||||
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -61,9 +61,6 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
private static int numTearDownCalls = 0;
|
||||
private static int numTearDownCallsInTransaction = 0;
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@@ -71,25 +68,26 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
private Pet pet;
|
||||
|
||||
@Autowired(required = false)
|
||||
protected Long nonrequiredLong;
|
||||
private Long nonrequiredLong;
|
||||
|
||||
@Resource()
|
||||
protected String foo;
|
||||
@Resource
|
||||
private String foo;
|
||||
|
||||
protected String bar;
|
||||
private String bar;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
|
||||
private int createPerson(String name) {
|
||||
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
@Autowired
|
||||
private void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
private int deletePerson(String name) {
|
||||
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.beanInitialized = true;
|
||||
@Resource
|
||||
private void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,24 +95,11 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
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
|
||||
void beforeClass() {
|
||||
@@ -132,12 +117,51 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
void setUp() {
|
||||
numSetUpCalls++;
|
||||
if (inTransaction()) {
|
||||
numSetUpCallsInTransaction++;
|
||||
}
|
||||
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
void tearDown() {
|
||||
numTearDownCalls++;
|
||||
if (inTransaction()) {
|
||||
numTearDownCallsInTransaction++;
|
||||
}
|
||||
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
void beforeTransaction() {
|
||||
assertNumRowsInPersonTable(1, "before a transactional test method");
|
||||
assertAddPerson(YODA);
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
void afterTransaction() {
|
||||
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
|
||||
assertNumRowsInPersonTable(1, "after a transactional test method");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
void verifyBeanNameSet() {
|
||||
assertInTransaction(false);
|
||||
assertTrue(this.beanName.startsWith(getClass().getName()), "The bean name of this test instance " +
|
||||
"should have been set to the fully qualified class name due to BeanNameAware semantics.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
void verifyApplicationContextSet() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull(super.applicationContext,
|
||||
"The application context should have been set due to ApplicationContextAware semantics.");
|
||||
"The application context should have been set due to ApplicationContextAware semantics.");
|
||||
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
|
||||
assertEquals(employeeBean.getName(), "John Smith", "employee's name.");
|
||||
}
|
||||
@@ -147,15 +171,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
void verifyBeanInitialized() {
|
||||
assertInTransaction(false);
|
||||
assertTrue(beanInitialized,
|
||||
"This test instance should have been initialized due to InitializingBean semantics.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
void verifyBeanNameSet() {
|
||||
assertInTransaction(false);
|
||||
assertEquals(beanName, getClass().getName(),
|
||||
"The bean name of this test instance should have been set due to BeanNameAware semantics.");
|
||||
"This test instance should have been initialized due to InitializingBean semantics.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -189,21 +205,6 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
assertEquals(bar, "Bar", "The setBar() method should have been injected via @Resource.");
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
void beforeTransaction() {
|
||||
assertNumRowsInPersonTable(1, "before a transactional test method");
|
||||
assertAddPerson(YODA);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
void setUp() throws Exception {
|
||||
numSetUpCalls++;
|
||||
if (inTransaction()) {
|
||||
numSetUpCallsInTransaction++;
|
||||
}
|
||||
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
@@ -212,19 +213,22 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
|
||||
assertNumRowsInPersonTable(4, "in modifyTestDataWithinTransaction()");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
void tearDown() throws Exception {
|
||||
numTearDownCalls++;
|
||||
if (inTransaction()) {
|
||||
numTearDownCallsInTransaction++;
|
||||
}
|
||||
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
|
||||
|
||||
private int createPerson(String name) {
|
||||
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
void afterTransaction() {
|
||||
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
|
||||
assertNumRowsInPersonTable(1, "after a transactional test method");
|
||||
private int deletePerson(String name) {
|
||||
return jdbcTemplate.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(String name) {
|
||||
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
import org.springframework.test.transaction.TransactionTestUtils;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -51,7 +52,7 @@ import static org.junit.Assert.*;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@DirtiesContext
|
||||
public class PrimaryTransactionManagerTests {
|
||||
public final class PrimaryTransactionManagerTests {
|
||||
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@@ -90,6 +91,7 @@ public class PrimaryTransactionManagerTests {
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement // SPR-17137: should not break trying to proxy the final test class
|
||||
static class Config {
|
||||
|
||||
@Primary
|
||||
|
||||
Reference in New Issue
Block a user