Skip to content

Commit 3d9b523

Browse files
committed
[ci] Use assertj/fluent exceptions for cleaner unit testing
1 parent 4d57247 commit 3d9b523

35 files changed

+493
-545
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@
251251
<version>9.1-901-1.jdbc4</version>
252252
<scope>test</scope>
253253
</dependency>
254+
<dependency>
255+
<groupId>org.assertj</groupId>
256+
<artifactId>assertj-core</artifactId>
257+
<version>2.6.0</version>
258+
<scope>test</scope>
259+
</dependency>
260+
<dependency>
261+
<groupId>eu.codearte.catch-exception</groupId>
262+
<artifactId>catch-exception</artifactId>
263+
<version>1.4.4</version>
264+
<scope>test</scope>
265+
</dependency>
254266
</dependencies>
255267

256268
<build>

src/test/java/org/apache/ibatis/autoconstructor/AutoConstructorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.ibatis.session.SqlSession;
2222
import org.apache.ibatis.session.SqlSessionFactory;
2323
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24-
import org.hamcrest.CoreMatchers;
24+
import org.assertj.core.api.Assertions;
2525
import org.junit.Assert;
2626
import org.junit.BeforeClass;
2727
import org.junit.Test;
@@ -109,6 +109,6 @@ public void badSubject() {
109109

110110
private void verifySubjects(final List<?> subjects) {
111111
Assert.assertNotNull(subjects);
112-
Assert.assertThat(subjects.size(), CoreMatchers.equalTo(3));
112+
Assertions.assertThat(subjects.size()).isEqualTo(3);
113113
}
114114
}

src/test/java/org/apache/ibatis/binding/FlushTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,9 +31,8 @@
3131
import java.util.ArrayList;
3232
import java.util.List;
3333

34-
import static org.hamcrest.core.Is.is;
3534
import static org.junit.Assert.assertNotNull;
36-
import static org.junit.Assert.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThat;
3736

3837
public class FlushTest {
3938
private static SqlSessionFactory sqlSessionFactory;
@@ -75,8 +74,8 @@ public void invokeFlushStatementsViaMapper() {
7574
// test
7675
List<BatchResult> results = mapper.flush();
7776

78-
assertThat(results.size(), is(1));
79-
assertThat(results.get(0).getUpdateCounts().length, is(ids.size()));
77+
assertThat(results.size()).isEqualTo(1);
78+
assertThat(results.get(0).getUpdateCounts().length).isEqualTo(ids.size());
8079

8180
for (int id : ids) {
8281
Author selectedAuthor = mapper.selectAuthor(id);

src/test/java/org/apache/ibatis/builder/ParameterExpressionTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,13 @@
1717

1818
import java.util.Map;
1919
import org.junit.Assert;
20-
import org.junit.Rule;
2120
import org.junit.Test;
22-
import org.junit.rules.ExpectedException;
2321

24-
import static org.hamcrest.core.Is.is;
22+
import static com.googlecode.catchexception.apis.BDDCatchException.*;
23+
import static org.assertj.core.api.BDDAssertions.then;
2524

2625
public class ParameterExpressionTest {
2726

28-
@Rule
29-
public ExpectedException expectedException = ExpectedException.none();
30-
3127
@Test
3228
public void simpleProperty() {
3329
Map<String, String> result = new ParameterExpression("id");
@@ -133,16 +129,20 @@ public void shouldIgnoreLeadingAndTrailingSpaces() {
133129

134130
@Test
135131
public void invalidOldJdbcTypeFormat() {
136-
expectedException.expect(BuilderException.class);
137-
expectedException.expectMessage(is("Parsing error in {id:} in position 3"));
138-
new ParameterExpression("id:");
132+
try {
133+
new ParameterExpression("id:");
134+
} catch (BuilderException e) {
135+
e.getMessage().contains("Parsing error in {id:} in position 3");
136+
}
139137
}
140138

141139
@Test
142140
public void invalidJdbcTypeOptUsingExpression() {
143-
expectedException.expect(BuilderException.class);
144-
expectedException.expectMessage(is("Parsing error in {(expression)+} in position 12"));
145-
new ParameterExpression("(expression)+");
141+
try {
142+
new ParameterExpression("(expression)+");
143+
} catch (BuilderException e) {
144+
e.getMessage().contains("Parsing error in {(expression)+} in position 12");
145+
}
146146
}
147147

148148
}

src/test/java/org/apache/ibatis/builder/XmlConfigBuilderTest.java

Lines changed: 91 additions & 92 deletions
Large diffs are not rendered by default.

src/test/java/org/apache/ibatis/builder/XmlMapperBuilderTest.java

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,17 +25,14 @@
2525
import org.apache.ibatis.mapping.StatementType;
2626
import org.apache.ibatis.session.Configuration;
2727
import org.apache.ibatis.type.TypeHandler;
28-
import org.junit.Rule;
2928
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
3129

32-
import static org.junit.Assert.assertThat;
33-
import static org.hamcrest.CoreMatchers.*;
30+
import static com.googlecode.catchexception.apis.BDDCatchException.*;
31+
import static org.assertj.core.api.BDDAssertions.then;
3432

35-
public class XmlMapperBuilderTest {
33+
import static org.assertj.core.api.Assertions.assertThat;
3634

37-
@Rule
38-
public ExpectedException expectedException = ExpectedException.none();
35+
public class XmlMapperBuilderTest {
3936

4037
@Test
4138
public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
@@ -44,6 +41,7 @@ public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
4441
InputStream inputStream = Resources.getResourceAsStream(resource);
4542
XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
4643
builder.parse();
44+
inputStream.close();
4745
}
4846

4947
@Test
@@ -55,110 +53,118 @@ public void mappedStatementWithOptions() throws Exception {
5553
builder.parse();
5654

5755
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions");
58-
assertThat(mappedStatement.getFetchSize(), is(200));
59-
assertThat(mappedStatement.getTimeout(), is(10));
60-
assertThat(mappedStatement.getStatementType(), is(StatementType.PREPARED));
61-
assertThat(mappedStatement.getResultSetType(), is(ResultSetType.SCROLL_SENSITIVE));
62-
assertThat(mappedStatement.isFlushCacheRequired(), is(false));
63-
assertThat(mappedStatement.isUseCache(), is(false));
64-
56+
assertThat(mappedStatement.getFetchSize()).isEqualTo(200);
57+
assertThat(mappedStatement.getTimeout()).isEqualTo(10);
58+
assertThat(mappedStatement.getStatementType()).isEqualTo(StatementType.PREPARED);
59+
assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_SENSITIVE);
60+
assertThat(mappedStatement.isFlushCacheRequired()).isFalse();
61+
assertThat(mappedStatement.isUseCache()).isFalse();
62+
inputStream.close();
6563
}
6664

6765
@Test
6866
public void parseExpression() {
6967
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
7068
{
7169
Pattern pattern = builder.parseExpression("[0-9]", "[a-z]");
72-
assertThat(pattern.matcher("0").find(), is(true));
73-
assertThat(pattern.matcher("a").find(), is(false));
70+
assertThat(pattern.matcher("0").find()).isTrue();
71+
assertThat(pattern.matcher("a").find()).isFalse();
7472
}
7573
{
7674
Pattern pattern = builder.parseExpression(null, "[a-z]");
77-
assertThat(pattern.matcher("0").find(), is(false));
78-
assertThat(pattern.matcher("a").find(), is(true));
75+
assertThat(pattern.matcher("0").find()).isFalse();
76+
assertThat(pattern.matcher("a").find()).isTrue();
7977
}
8078
}
8179

8280
@Test
8381
public void resolveJdbcTypeWithUndefinedValue() {
8482
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
85-
expectedException.expect(BuilderException.class);
86-
expectedException.expectMessage(startsWith("Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum"));
87-
expectedException.expectMessage(endsWith("org.apache.ibatis.type.JdbcType.aaa"));
88-
builder.resolveJdbcType("aaa");
83+
when(builder).resolveJdbcType("aaa");
84+
then(caughtException())
85+
.isInstanceOf(BuilderException.class)
86+
.hasMessageStartingWith("Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum")
87+
.hasMessageEndingWith("org.apache.ibatis.type.JdbcType.aaa");
8988
}
9089

9190
@Test
9291
public void resolveResultSetTypeWithUndefinedValue() {
9392
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
94-
expectedException.expect(BuilderException.class);
95-
expectedException.expectMessage(startsWith("Error resolving ResultSetType. Cause: java.lang.IllegalArgumentException: No enum"));
96-
expectedException.expectMessage(endsWith("org.apache.ibatis.mapping.ResultSetType.bbb"));
97-
builder.resolveResultSetType("bbb");
93+
when(builder).resolveResultSetType("bbb");
94+
then(caughtException())
95+
.isInstanceOf(BuilderException.class)
96+
.hasMessageStartingWith("Error resolving ResultSetType. Cause: java.lang.IllegalArgumentException: No enum")
97+
.hasMessageEndingWith("org.apache.ibatis.mapping.ResultSetType.bbb");
9898
}
9999

100100
@Test
101101
public void resolveParameterModeWithUndefinedValue() {
102102
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
103-
expectedException.expect(BuilderException.class);
104-
expectedException.expectMessage(startsWith("Error resolving ParameterMode. Cause: java.lang.IllegalArgumentException: No enum"));
105-
expectedException.expectMessage(endsWith("org.apache.ibatis.mapping.ParameterMode.ccc"));
106-
builder.resolveParameterMode("ccc");
103+
when(builder).resolveParameterMode("ccc");
104+
then(caughtException())
105+
.isInstanceOf(BuilderException.class)
106+
.hasMessageStartingWith("Error resolving ParameterMode. Cause: java.lang.IllegalArgumentException: No enum")
107+
.hasMessageEndingWith("org.apache.ibatis.mapping.ParameterMode.ccc");
107108
}
108109

109110
@Test
110111
public void createInstanceWithAbstractClass() {
111112
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
112-
expectedException.expect(BuilderException.class);
113-
expectedException.expectMessage(is("Error creating instance. Cause: java.lang.InstantiationException: org.apache.ibatis.builder.BaseBuilder"));
114-
builder.createInstance("org.apache.ibatis.builder.BaseBuilder");
113+
when(builder).createInstance("org.apache.ibatis.builder.BaseBuilder");
114+
then(caughtException())
115+
.isInstanceOf(BuilderException.class)
116+
.hasMessage("Error creating instance. Cause: java.lang.InstantiationException: org.apache.ibatis.builder.BaseBuilder");
115117
}
116118

117119
@Test
118120
public void resolveClassWithNotFound() {
119121
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
120-
expectedException.expect(BuilderException.class);
121-
expectedException.expectMessage(is("Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'ddd'. Cause: java.lang.ClassNotFoundException: Cannot find class: ddd"));
122-
builder.resolveClass("ddd");
122+
when(builder).resolveClass("ddd");
123+
then(caughtException())
124+
.isInstanceOf(BuilderException.class)
125+
.hasMessage("Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'ddd'. Cause: java.lang.ClassNotFoundException: Cannot find class: ddd");
123126
}
124127

125128
@Test
126129
public void resolveTypeHandlerTypeHandlerAliasIsNull() {
127130
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
128131
TypeHandler<?> typeHandler = builder.resolveTypeHandler(String.class, (String)null);
129-
assertThat(typeHandler, nullValue());
132+
assertThat(typeHandler).isNull();
130133
}
131134

132135
@Test
133136
public void resolveTypeHandlerNoAssignable() {
134137
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
135-
expectedException.expect(BuilderException.class);
136-
expectedException.expectMessage(is("Type java.lang.Integer is not a valid TypeHandler because it does not implement TypeHandler interface"));
137-
builder.resolveTypeHandler(String.class, "integer");
138+
when(builder).resolveTypeHandler(String.class, "integer");
139+
then(caughtException())
140+
.isInstanceOf(BuilderException.class)
141+
.hasMessage("Type java.lang.Integer is not a valid TypeHandler because it does not implement TypeHandler interface");
138142
}
139143

140144
@Test
141145
public void setCurrentNamespaceValueIsNull() {
142146
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
143-
expectedException.expect(BuilderException.class);
144-
expectedException.expectMessage(is("The mapper element requires a namespace attribute to be specified."));
145-
builder.setCurrentNamespace(null);
147+
when(builder).setCurrentNamespace(null);
148+
then(caughtException())
149+
.isInstanceOf(BuilderException.class)
150+
.hasMessage("The mapper element requires a namespace attribute to be specified.");
146151
}
147152

148153
@Test
149154
public void useCacheRefNamespaceIsNull() {
150155
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
151-
expectedException.expect(BuilderException.class);
152-
expectedException.expectMessage(is("cache-ref element requires a namespace attribute."));
153-
builder.useCacheRef(null);
156+
when(builder).useCacheRef(null);
157+
then(caughtException())
158+
.isInstanceOf(BuilderException.class)
159+
.hasMessage("cache-ref element requires a namespace attribute.");
154160
}
155161

156162
@Test
157163
public void useCacheRefNamespaceIsUndefined() {
158164
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
159-
expectedException.expect(IncompleteElementException.class);
160-
expectedException.expectMessage(is("No cache for namespace 'eee' could be found."));
161-
builder.useCacheRef("eee");
165+
when(builder).useCacheRef("eee");
166+
then(caughtException())
167+
.hasMessage("No cache for namespace 'eee' could be found.");
162168
}
163169

164170
// @Test

src/test/java/org/apache/ibatis/executor/ResultExtractorTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
import java.util.*;
2828

29-
import static org.hamcrest.CoreMatchers.*;
30-
import static org.junit.Assert.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThat;
3130
import static org.mockito.Mockito.*;
3231

3332
@RunWith(MockitoJUnitRunner.class)
@@ -48,25 +47,25 @@ public void setUp() throws Exception {
4847
@Test
4948
public void shouldExtractNullForNullTargetType() {
5049
final Object result = resultExtractor.extractObjectFromList(null, null);
51-
assertThat(result, nullValue());
50+
assertThat(result).isNull();
5251
}
5352

5453
@Test
5554
public void shouldExtractList() {
5655
final List list = Arrays.asList(1, 2, 3);
5756
final Object result = resultExtractor.extractObjectFromList(list, List.class);
58-
assertThat(result, instanceOf(List.class));
57+
assertThat(result).isInstanceOf(List.class);
5958
final List resultList = (List) result;
60-
assertThat(resultList, equalTo(list));
59+
assertThat(resultList).isEqualTo(list);
6160
}
6261

6362
@Test
6463
public void shouldExtractArray() {
6564
final List list = Arrays.asList(1, 2, 3);
6665
final Object result = resultExtractor.extractObjectFromList(list, Integer[].class);
67-
assertThat(result, instanceOf(Integer[].class));
66+
assertThat(result).isInstanceOf(Integer[].class);
6867
final Integer[] resultArray = (Integer[]) result;
69-
assertThat(resultArray, equalTo(new Integer[]{1, 2, 3}));
68+
assertThat(resultArray).isEqualTo(new Integer[]{1, 2, 3});
7069
}
7170

7271
@Test
@@ -80,22 +79,22 @@ public void shouldExtractSet() {
8079
when(configuration.newMetaObject(set)).thenReturn(metaObject);
8180

8281
final Set result = (Set) resultExtractor.extractObjectFromList(list, targetType);
83-
assertThat(result, sameInstance(set));
82+
assertThat(result).isSameAs(set);
8483

8584
verify(metaObject).addAll(list);
8685
}
8786

8887
@Test
8988
public void shouldExtractSingleObject() {
9089
final List list = Collections.singletonList("single object");
91-
assertThat((String) resultExtractor.extractObjectFromList(list, String.class), equalTo("single object"));
92-
assertThat((String) resultExtractor.extractObjectFromList(list, null), equalTo("single object"));
93-
assertThat((String) resultExtractor.extractObjectFromList(list, Integer.class), equalTo("single object"));
90+
assertThat((String) resultExtractor.extractObjectFromList(list, String.class)).isEqualTo("single object");
91+
assertThat((String) resultExtractor.extractObjectFromList(list, null)).isEqualTo("single object");
92+
assertThat((String) resultExtractor.extractObjectFromList(list, Integer.class)).isEqualTo("single object");
9493
}
9594

9695
@Test(expected = ExecutorException.class)
9796
public void shouldFailWhenMutipleItemsInList() {
9897
final List list = Arrays.asList("first object", "second object");
99-
assertThat((String) resultExtractor.extractObjectFromList(list, String.class), equalTo("single object"));
98+
assertThat((String) resultExtractor.extractObjectFromList(list, String.class)).isEqualTo("single object");
10099
}
101100
}

0 commit comments

Comments
 (0)