Skip to content

Commit 4f4427f

Browse files
committed
Migrate TestNG assertions to AssertJ
Migrate all existing TestNG based assertions to AssertJ and add Checkstyle rules to ensure they don't return. See gh-23022
1 parent 50cc23c commit 4f4427f

10 files changed

+87
-74
lines changed

spring-test/src/test/java/org/springframework/test/context/cache/ClassLevelDirtiesContextTestNGTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ static class Config {
182182

183183

184184
protected void assertApplicationContextWasAutowired() {
185-
org.testng.Assert.assertNotNull(this.applicationContext,
186-
"The application context should have been autowired.");
185+
assertThat(this.applicationContext).as("The application context should have been autowired.").isNotNull();
187186
}
188187
}
189188

spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTestNGSpringContextTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
import org.springframework.tests.sample.beans.Employee;
2626
import org.springframework.tests.sample.beans.Pet;
2727

28-
import static org.testng.Assert.assertEquals;
29-
import static org.testng.Assert.assertNotNull;
28+
import static org.assertj.core.api.Assertions.assertThat;
3029

3130
/**
3231
* Integration tests that verify support for
@@ -50,11 +49,11 @@ public class AnnotationConfigTestNGSpringContextTests extends AbstractTestNGSpri
5049

5150
@Test
5251
void autowiringFromConfigClass() {
53-
assertNotNull(employee, "The employee should have been autowired.");
54-
assertEquals(employee.getName(), "John Smith");
52+
assertThat(employee).as("The employee should have been autowired.").isNotNull();
53+
assertThat(employee.getName()).isEqualTo("John Smith");
5554

56-
assertNotNull(pet, "The pet should have been autowired.");
57-
assertEquals(pet.getName(), "Fido");
55+
assertThat(pet).as("The pet should have been autowired.").isNotNull();
56+
assertThat(pet.getName()).isEqualTo("Fido");
5857
}
5958

6059

spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
import org.springframework.transaction.annotation.Propagation;
3939
import org.springframework.transaction.annotation.Transactional;
4040

41+
import static org.assertj.core.api.Assertions.assertThat;
4142
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
4243
import static org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive;
43-
import static org.testng.Assert.assertEquals;
44-
import static org.testng.Assert.assertNotNull;
4544

4645
/**
4746
* Integration tests that verify support for
@@ -86,12 +85,13 @@ private int deletePerson(String name) {
8685
}
8786

8887
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
89-
assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
90-
+ testState + ").");
88+
assertThat(countRowsInTable("person"))
89+
.as("the number of rows in the person table (" + testState + ").")
90+
.isEqualTo(expectedNumRows);
9191
}
9292

93-
private void assertAddPerson(final String name) {
94-
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
93+
private void assertAddPerson(String name) {
94+
assertThat(createPerson(name)).as("Adding '%s'", name).isEqualTo(1);
9595
}
9696

9797
@BeforeClass
@@ -104,20 +104,20 @@ void beforeClass() {
104104

105105
@AfterClass
106106
void afterClass() {
107-
assertEquals(numSetUpCalls, NUM_TESTS, "number of calls to setUp().");
108-
assertEquals(numSetUpCallsInTransaction, NUM_TX_TESTS, "number of calls to setUp() within a transaction.");
109-
assertEquals(numTearDownCalls, NUM_TESTS, "number of calls to tearDown().");
110-
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
107+
assertThat(numSetUpCalls).as("number of calls to setUp().").isEqualTo(NUM_TESTS);
108+
assertThat(numSetUpCallsInTransaction).as("number of calls to setUp() within a transaction.").isEqualTo(NUM_TX_TESTS);
109+
assertThat(numTearDownCalls).as("number of calls to tearDown().").isEqualTo(NUM_TESTS);
110+
assertThat(numTearDownCallsInTransaction).as("number of calls to tearDown() within a transaction.").isEqualTo(NUM_TX_TESTS);
111111
}
112112

113113
@Test
114114
@Transactional(propagation = Propagation.NOT_SUPPORTED)
115115
void autowiringFromConfigClass() {
116-
assertNotNull(employee, "The employee should have been autowired.");
117-
assertEquals(employee.getName(), "John Smith");
116+
assertThat(employee).as("The employee should have been autowired.").isNotNull();
117+
assertThat(employee.getName()).isEqualTo("John Smith");
118118

119-
assertNotNull(pet, "The pet should have been autowired.");
120-
assertEquals(pet.getName(), "Fido");
119+
assertThat(pet).as("The pet should have been autowired.").isNotNull();
120+
assertThat(pet.getName()).isEqualTo("Fido");
121121
}
122122

123123
@BeforeTransaction
@@ -154,7 +154,7 @@ void tearDown() throws Exception {
154154

155155
@AfterTransaction
156156
void afterTransaction() {
157-
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
157+
assertThat(deletePerson(YODA)).as("Deleting yoda").isEqualTo(1);
158158
assertNumRowsInPersonTable(1, "after a transactional test method");
159159
}
160160

spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@
3535
import org.springframework.transaction.annotation.Propagation;
3636
import org.springframework.transaction.annotation.Transactional;
3737

38+
import static org.assertj.core.api.Assertions.assertThat;
3839
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
3940
import static org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive;
40-
import static org.testng.Assert.assertEquals;
41-
import static org.testng.Assert.assertNotNull;
42-
import static org.testng.Assert.assertNull;
43-
import static org.testng.Assert.assertTrue;
4441

4542
/**
4643
* Combined integration test for {@link AbstractTestNGSpringContextTests} and
@@ -115,10 +112,10 @@ void beforeClass() {
115112

116113
@AfterClass
117114
void afterClass() {
118-
assertEquals(numSetUpCalls, NUM_TESTS, "number of calls to setUp().");
119-
assertEquals(numSetUpCallsInTransaction, NUM_TX_TESTS, "number of calls to setUp() within a transaction.");
120-
assertEquals(numTearDownCalls, NUM_TESTS, "number of calls to tearDown().");
121-
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
115+
assertThat(numSetUpCalls).as("number of calls to setUp().").isEqualTo(NUM_TESTS);
116+
assertThat(numSetUpCallsInTransaction).as("number of calls to setUp() within a transaction.").isEqualTo(NUM_TX_TESTS);
117+
assertThat(numTearDownCalls).as("number of calls to tearDown().").isEqualTo(NUM_TESTS);
118+
assertThat(numTearDownCallsInTransaction).as("number of calls to tearDown() within a transaction.").isEqualTo(NUM_TX_TESTS);
122119
}
123120

124121
@BeforeMethod
@@ -147,7 +144,7 @@ void beforeTransaction() {
147144

148145
@AfterTransaction
149146
void afterTransaction() {
150-
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
147+
assertThat(deletePerson(YODA)).as("Deleting yoda").isEqualTo(1);
151148
assertNumRowsInPersonTable(1, "after a transactional test method");
152149
}
153150

@@ -156,57 +153,60 @@ void afterTransaction() {
156153
@Transactional(propagation = Propagation.NOT_SUPPORTED)
157154
void verifyBeanNameSet() {
158155
assertThatTransaction().isNotActive();
159-
assertTrue(this.beanName.startsWith(getClass().getName()), "The bean name of this test instance " +
160-
"should have been set to the fully qualified class name due to BeanNameAware semantics.");
156+
assertThat(this.beanName)
157+
.as("The bean name of this test instance should have been set to the fully qualified class name due to BeanNameAware semantics.")
158+
.startsWith(getClass().getName());
161159
}
162160

163161
@Test
164162
@Transactional(propagation = Propagation.NOT_SUPPORTED)
165163
void verifyApplicationContextSet() {
166164
assertThatTransaction().isNotActive();
167-
assertNotNull(super.applicationContext,
168-
"The application context should have been set due to ApplicationContextAware semantics.");
165+
assertThat(super.applicationContext)
166+
.as("The application context should have been set due to ApplicationContextAware semantics.")
167+
.isNotNull();
169168
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
170-
assertEquals(employeeBean.getName(), "John Smith", "employee's name.");
169+
assertThat(employeeBean.getName()).as("employee's name.").isEqualTo("John Smith");
171170
}
172171

173172
@Test
174173
@Transactional(propagation = Propagation.NOT_SUPPORTED)
175174
void verifyBeanInitialized() {
176175
assertThatTransaction().isNotActive();
177-
assertTrue(beanInitialized,
178-
"This test instance should have been initialized due to InitializingBean semantics.");
176+
assertThat(beanInitialized)
177+
.as("This test instance should have been initialized due to InitializingBean semantics.")
178+
.isTrue();
179179
}
180180

181181
@Test
182182
@Transactional(propagation = Propagation.NOT_SUPPORTED)
183183
void verifyAnnotationAutowiredFields() {
184184
assertThatTransaction().isNotActive();
185-
assertNull(nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
186-
assertNotNull(pet, "The pet field should have been autowired.");
187-
assertEquals(pet.getName(), "Fido", "pet's name.");
185+
assertThat(nonrequiredLong).as("The nonrequiredLong field should NOT have been autowired.").isNull();
186+
assertThat(pet).as("The pet field should have been autowired.").isNotNull();
187+
assertThat(pet.getName()).as("pet's name.").isEqualTo("Fido");
188188
}
189189

190190
@Test
191191
@Transactional(propagation = Propagation.NOT_SUPPORTED)
192192
void verifyAnnotationAutowiredMethods() {
193193
assertThatTransaction().isNotActive();
194-
assertNotNull(employee, "The setEmployee() method should have been autowired.");
195-
assertEquals(employee.getName(), "John Smith", "employee's name.");
194+
assertThat(employee).as("The setEmployee() method should have been autowired.").isNotNull();
195+
assertThat(employee.getName()).as("employee's name.").isEqualTo("John Smith");
196196
}
197197

198198
@Test
199199
@Transactional(propagation = Propagation.NOT_SUPPORTED)
200200
void verifyResourceAnnotationInjectedFields() {
201201
assertThatTransaction().isNotActive();
202-
assertEquals(foo, "Foo", "The foo field should have been injected via @Resource.");
202+
assertThat(foo).as("The foo field should have been injected via @Resource.").isEqualTo("Foo");
203203
}
204204

205205
@Test
206206
@Transactional(propagation = Propagation.NOT_SUPPORTED)
207207
void verifyResourceAnnotationInjectedMethods() {
208208
assertThatTransaction().isNotActive();
209-
assertEquals(bar, "Bar", "The setBar() method should have been injected via @Resource.");
209+
assertThat(bar).as("The setBar() method should have been injected via @Resource.").isEqualTo("Bar");
210210
}
211211

212212
@Test
@@ -227,12 +227,13 @@ private int deletePerson(String name) {
227227
}
228228

229229
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
230-
assertEquals(countRowsInTable("person"), expectedNumRows,
231-
"the number of rows in the person table (" + testState + ").");
230+
assertThat(countRowsInTable("person"))
231+
.as("the number of rows in the person table (" + testState + ").")
232+
.isEqualTo(expectedNumRows);
232233
}
233234

234235
private void assertAddPerson(String name) {
235-
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
236+
assertThat(createPerson(name)).as("Adding '" + name + "'").isEqualTo(1);
236237
}
237238

238239
}

spring-test/src/test/java/org/springframework/test/context/testng/DirtiesContextTransactionalTestNGSpringContextTests.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import org.springframework.test.context.ContextConfiguration;
2424
import org.springframework.test.context.TestContextManager;
2525

26+
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
27-
import static org.testng.Assert.assertNotNull;
28-
import static org.testng.Assert.assertNotSame;
29-
import static org.testng.Assert.assertSame;
3028

3129
/**
3230
* <p>
@@ -56,10 +54,12 @@ public class DirtiesContextTransactionalTestNGSpringContextTests extends Abstrac
5654

5755
private void performCommonAssertions() {
5856
assertThatTransaction().isActive();
59-
assertNotNull(super.applicationContext,
60-
"The application context should have been set due to ApplicationContextAware semantics.");
61-
assertNotNull(super.jdbcTemplate,
62-
"The JdbcTemplate should have been created in setDataSource() via DI for the DataSource.");
57+
assertThat(super.applicationContext)
58+
.as("The application context should have been set due to ApplicationContextAware semantics.")
59+
.isNotNull();
60+
assertThat(super.jdbcTemplate)
61+
.as("The JdbcTemplate should have been created in setDataSource() via DI for the DataSource.")
62+
.isNotNull();
6363
}
6464

6565
@Test
@@ -72,15 +72,17 @@ public void dirtyContext() {
7272
@Test(dependsOnMethods = { "dirtyContext" })
7373
public void verifyContextWasDirtied() {
7474
performCommonAssertions();
75-
assertNotSame(super.applicationContext, this.dirtiedApplicationContext,
76-
"The application context should have been 'dirtied'.");
75+
assertThat(super.applicationContext)
76+
.as("The application context should have been 'dirtied'.")
77+
.isNotSameAs(this.dirtiedApplicationContext);
7778
this.dirtiedApplicationContext = super.applicationContext;
7879
}
7980

8081
@Test(dependsOnMethods = { "verifyContextWasDirtied" })
8182
public void verifyContextWasNotDirtied() {
82-
assertSame(this.applicationContext, this.dirtiedApplicationContext,
83-
"The application context should NOT have been 'dirtied'.");
83+
assertThat(this.applicationContext)
84+
.as("The application context should NOT have been 'dirtied'.")
85+
.isSameAs(this.dirtiedApplicationContext);
8486
}
8587

8688
}

spring-test/src/test/java/org/springframework/test/context/testng/transaction/ejb/AbstractEjbTxDaoTestNGTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
2929
import org.springframework.test.context.transaction.ejb.dao.TestEntityDao;
3030

31-
import static org.testng.AssertJUnit.assertEquals;
31+
import static org.assertj.core.api.Assertions.assertThat;
3232

3333
/**
3434
* Abstract base class for all TestNG-based tests involving EJB transaction
@@ -53,13 +53,13 @@ public abstract class AbstractEjbTxDaoTestNGTests extends AbstractTransactionalT
5353
@Test
5454
public void test1InitialState() {
5555
int count = dao.getCount(TEST_NAME);
56-
assertEquals("New TestEntity should have count=0.", 0, count);
56+
assertThat(count).as("New TestEntity should have count=0.").isEqualTo(0);
5757
}
5858

5959
@Test(dependsOnMethods = "test1InitialState")
6060
public void test2IncrementCount1() {
6161
int count = dao.incrementCount(TEST_NAME);
62-
assertEquals("Expected count=1 after first increment.", 1, count);
62+
assertThat(count).as("Expected count=1 after first increment.").isEqualTo(1);
6363
}
6464

6565
/**
@@ -70,10 +70,10 @@ public void test2IncrementCount1() {
7070
@Test(dependsOnMethods = "test2IncrementCount1")
7171
public void test3IncrementCount2() {
7272
int count = dao.getCount(TEST_NAME);
73-
assertEquals("Expected count=1 after test2IncrementCount1().", 1, count);
73+
assertThat(count).as("Expected count=1 after test2IncrementCount1().").isEqualTo(1);
7474

7575
count = dao.incrementCount(TEST_NAME);
76-
assertEquals("Expected count=2 now.", 2, count);
76+
assertThat(count).as("Expected count=2 now.").isEqualTo(2);
7777
}
7878

7979
@AfterMethod(alwaysRun = true)

spring-test/src/test/java/org/springframework/test/context/testng/transaction/ejb/RollbackForRequiredEjbTxDaoTestNGTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.springframework.test.annotation.Rollback;
2222
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
2323

24-
import static org.testng.AssertJUnit.assertEquals;
24+
import static org.assertj.core.api.Assertions.assertThat;
2525

2626
/**
2727
* Extension of {@link CommitForRequiredEjbTxDaoTestNGTests} which sets the default
@@ -52,10 +52,10 @@ public void test3IncrementCount2() {
5252
// participate in the existing transaction (if present), which in this case is the
5353
// transaction managed by the TestContext framework which will be rolled back
5454
// after each test method.
55-
assertEquals("Expected count=0 after test2IncrementCount1().", 0, count);
55+
assertThat(count).as("Expected count=0 after test2IncrementCount1().").isEqualTo(0);
5656

5757
count = dao.incrementCount(TEST_NAME);
58-
assertEquals("Expected count=1 now.", 1, count);
58+
assertThat(count).as("Expected count=1 now.").isEqualTo(1);
5959
}
6060

6161
}

spring-test/src/test/java/org/springframework/test/context/testng/web/ServletTestExecutionListenerTestNGIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ void ensureMocksAreReinjectedBetweenTests_2() {
7272
}
7373

7474
private void assertInjectedServletRequestEqualsRequestInRequestContextHolder() {
75-
assertThat(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()).as("Injected ServletRequest must be stored in the RequestContextHolder").isEqualTo(servletRequest);
75+
assertThat(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest())
76+
.as("Injected ServletRequest must be stored in the RequestContextHolder")
77+
.isEqualTo(servletRequest);
7678
}
7779

7880
}

spring-test/src/test/java/org/springframework/test/context/testng/web/TestNGSpringContextWebTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ void basicWacFeatures() throws Exception {
100100
assertThat(webRequest).as("ServletWebRequest should have been autowired from the WAC.").isNotNull();
101101

102102
Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
103-
assertThat(rootWac).as("Root WAC must be stored in the ServletContext as: "
104-
+ WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE).isNotNull();
103+
assertThat(rootWac)
104+
.as("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)
105+
.isNotNull();
105106
assertThat(rootWac).as("test WAC and Root WAC in ServletContext must be the same object.").isSameAs(wac);
106107
assertThat(wac.getServletContext()).as("ServletContext instances must be the same object.").isSameAs(mockServletContext);
107108
assertThat(request.getServletContext()).as("ServletContext in the WAC and in the mock request").isSameAs(mockServletContext);
108109

109-
assertThat(mockServletContext.getRealPath("index.jsp")).as("Getting real path for ServletContext resource.").isEqualTo(new File("src/main/webapp/index.jsp").getCanonicalPath());
110-
110+
assertThat(mockServletContext.getRealPath("index.jsp"))
111+
.as("Getting real path for ServletContext resource.")
112+
.isEqualTo(new File("src/main/webapp/index.jsp").getCanonicalPath());
111113
}
112114

113115
@Test

0 commit comments

Comments
 (0)