Skip to content

Commit 3c8ee06

Browse files
committed
[ci] Use assertj/fluent exceptions for cleaner unit testing
1 parent deba316 commit 3c8ee06

15 files changed

+223
-226
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>3.6.1</version>
258+
<scope>test</scope>
259+
</dependency>
260+
<dependency>
261+
<groupId>pl.wkr</groupId>
262+
<artifactId>fluent-exception-rule</artifactId>
263+
<version>1.0.1</version>
264+
<scope>test</scope>
265+
</dependency>
254266
</dependencies>
255267

256268
<build>

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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import org.junit.Assert;
2020
import org.junit.Rule;
2121
import org.junit.Test;
22-
import org.junit.rules.ExpectedException;
2322

24-
import static org.hamcrest.core.Is.is;
23+
import pl.wkr.fluentrule.api.FluentExpectedException;
2524

2625
public class ParameterExpressionTest {
2726

2827
@Rule
29-
public ExpectedException expectedException = ExpectedException.none();
28+
public FluentExpectedException expectedException = FluentExpectedException.none();
3029

3130
@Test
3231
public void simpleProperty() {
@@ -133,15 +132,13 @@ public void shouldIgnoreLeadingAndTrailingSpaces() {
133132

134133
@Test
135134
public void invalidOldJdbcTypeFormat() {
136-
expectedException.expect(BuilderException.class);
137-
expectedException.expectMessage(is("Parsing error in {id:} in position 3"));
135+
expectedException.expect(BuilderException.class).hasMessage("Parsing error in {id:} in position 3");
138136
new ParameterExpression("id:");
139137
}
140138

141139
@Test
142140
public void invalidJdbcTypeOptUsingExpression() {
143-
expectedException.expect(BuilderException.class);
144-
expectedException.expectMessage(is("Parsing error in {(expression)+} in position 12"));
141+
expectedException.expect(BuilderException.class).hasMessage("Parsing error in {(expression)+} in position 12");
145142
new ParameterExpression("(expression)+");
146143
}
147144

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

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@
6060
import org.junit.Test;
6161
import org.junit.rules.ExpectedException;
6262

63-
import static org.hamcrest.core.Is.*;
64-
import static org.hamcrest.core.IsInstanceOf.*;
65-
import static org.junit.Assert.*;
63+
import static org.assertj.core.api.Assertions.assertThat;
64+
import static org.junit.Assert.assertNotNull;
65+
import static org.junit.Assert.assertNull;
66+
import static org.junit.Assert.assertTrue;
67+
import static org.junit.Assert.assertArrayEquals;
6668

6769
public class XmlConfigBuilderTest {
6870

@@ -76,26 +78,26 @@ public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
7678
XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
7779
Configuration config = builder.parse();
7880
assertNotNull(config);
79-
assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.PARTIAL));
80-
assertThat(config.getAutoMappingUnknownColumnBehavior(), is(AutoMappingUnknownColumnBehavior.NONE));
81-
assertThat(config.isCacheEnabled(), is(true));
82-
assertThat(config.getProxyFactory(), is(instanceOf(JavassistProxyFactory.class)));
83-
assertThat(config.isLazyLoadingEnabled(), is(false));
84-
assertThat(config.isAggressiveLazyLoading(), is(false));
85-
assertThat(config.isMultipleResultSetsEnabled(), is(true));
86-
assertThat(config.isUseColumnLabel(), is(true));
87-
assertThat(config.isUseGeneratedKeys(), is(false));
88-
assertThat(config.getDefaultExecutorType(), is(ExecutorType.SIMPLE));
81+
assertThat(config.getAutoMappingBehavior()).isEqualTo(AutoMappingBehavior.PARTIAL);
82+
assertThat(config.getAutoMappingUnknownColumnBehavior()).isEqualTo(AutoMappingUnknownColumnBehavior.NONE);
83+
assertThat(config.isCacheEnabled()).isTrue();
84+
assertThat(config.getProxyFactory()).isInstanceOf(JavassistProxyFactory.class);
85+
assertThat(config.isLazyLoadingEnabled()).isFalse();
86+
assertThat(config.isAggressiveLazyLoading()).isFalse();
87+
assertThat(config.isMultipleResultSetsEnabled()).isTrue();
88+
assertThat(config.isUseColumnLabel()).isTrue();
89+
assertThat(config.isUseGeneratedKeys()).isFalse();
90+
assertThat(config.getDefaultExecutorType()).isEqualTo(ExecutorType.SIMPLE);
8991
assertNull(config.getDefaultStatementTimeout());
9092
assertNull(config.getDefaultFetchSize());
91-
assertThat(config.isMapUnderscoreToCamelCase(), is(false));
92-
assertThat(config.isSafeRowBoundsEnabled(), is(false));
93-
assertThat(config.getLocalCacheScope(), is(LocalCacheScope.SESSION));
94-
assertThat(config.getJdbcTypeForNull(), is(JdbcType.OTHER));
95-
assertThat(config.getLazyLoadTriggerMethods(), is((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString"))));
96-
assertThat(config.isSafeResultHandlerEnabled(), is(true));
97-
assertThat(config.getDefaultScriptingLanguageInstance(), is(instanceOf(XMLLanguageDriver.class)));
98-
assertThat(config.isCallSettersOnNulls(), is(false));
93+
assertThat(config.isMapUnderscoreToCamelCase()).isFalse();
94+
assertThat(config.isSafeRowBoundsEnabled()).isFalse();
95+
assertThat(config.getLocalCacheScope()).isEqualTo(LocalCacheScope.SESSION);
96+
assertThat(config.getJdbcTypeForNull()).isEqualTo(JdbcType.OTHER);
97+
assertThat(config.getLazyLoadTriggerMethods()).isEqualTo((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString")));
98+
assertThat(config.isSafeResultHandlerEnabled()).isTrue();
99+
assertThat(config.getDefaultScriptingLanuageInstance()).isInstanceOf(XMLLanguageDriver.class);
100+
assertThat(config.isCallSettersOnNulls()).isFalse();
99101
assertNull(config.getLogPrefix());
100102
assertNull(config.getLogImpl());
101103
assertNull(config.getConfigurationFactory());
@@ -168,65 +170,65 @@ public void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
168170
XMLConfigBuilder builder = new XMLConfigBuilder(inputStream, null, props);
169171
Configuration config = builder.parse();
170172

171-
assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.NONE));
172-
assertThat(config.getAutoMappingUnknownColumnBehavior(), is(AutoMappingUnknownColumnBehavior.WARNING));
173-
assertThat(config.isCacheEnabled(), is(false));
174-
assertThat(config.getProxyFactory(), is(instanceOf(CglibProxyFactory.class)));
175-
assertThat(config.isLazyLoadingEnabled(), is(true));
176-
assertThat(config.isAggressiveLazyLoading(), is(true));
177-
assertThat(config.isMultipleResultSetsEnabled(), is(false));
178-
assertThat(config.isUseColumnLabel(), is(false));
179-
assertThat(config.isUseGeneratedKeys(), is(true));
180-
assertThat(config.getDefaultExecutorType(), is(ExecutorType.BATCH));
181-
assertThat(config.getDefaultStatementTimeout(), is(10));
182-
assertThat(config.getDefaultFetchSize(), is(100));
183-
assertThat(config.isMapUnderscoreToCamelCase(), is(true));
184-
assertThat(config.isSafeRowBoundsEnabled(), is(true));
185-
assertThat(config.getLocalCacheScope(), is(LocalCacheScope.STATEMENT));
186-
assertThat(config.getJdbcTypeForNull(), is(JdbcType.NULL));
187-
assertThat(config.getLazyLoadTriggerMethods(), is((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx"))));
188-
assertThat(config.isSafeResultHandlerEnabled(), is(false));
189-
assertThat(config.getDefaultScriptingLanguageInstance(), is(instanceOf(RawLanguageDriver.class)));
190-
assertThat(config.isCallSettersOnNulls(), is(true));
191-
assertThat(config.getLogPrefix(), is("mybatis_"));
192-
assertThat(config.getLogImpl().getName(), is(Slf4jImpl.class.getName()));
193-
assertThat(config.getVfsImpl().getName(), is(JBoss6VFS.class.getName()));
194-
assertThat(config.getConfigurationFactory().getName(), is(String.class.getName()));
173+
assertThat(config.getAutoMappingBehavior()).isEqualTo(AutoMappingBehavior.NONE);
174+
assertThat(config.getAutoMappingUnknownColumnBehavior()).isEqualTo(AutoMappingUnknownColumnBehavior.WARNING);
175+
assertThat(config.isCacheEnabled()).isFalse();
176+
assertThat(config.getProxyFactory()).isInstanceOf(CglibProxyFactory.class);
177+
assertThat(config.isLazyLoadingEnabled()).isTrue();
178+
assertThat(config.isAggressiveLazyLoading()).isTrue();
179+
assertThat(config.isMultipleResultSetsEnabled()).isFalse();
180+
assertThat(config.isUseColumnLabel()).isFalse();
181+
assertThat(config.isUseGeneratedKeys()).isTrue();
182+
assertThat(config.getDefaultExecutorType()).isEqualTo(ExecutorType.BATCH);
183+
assertThat(config.getDefaultStatementTimeout()).isEqualTo(10);
184+
assertThat(config.getDefaultFetchSize()).isEqualTo(100);
185+
assertThat(config.isMapUnderscoreToCamelCase()).isTrue();
186+
assertThat(config.isSafeRowBoundsEnabled()).isTrue();
187+
assertThat(config.getLocalCacheScope()).isEqualTo(LocalCacheScope.STATEMENT);
188+
assertThat(config.getJdbcTypeForNull()).isEqualTo(JdbcType.NULL);
189+
assertThat(config.getLazyLoadTriggerMethods()).isEqualTo((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx")));
190+
assertThat(config.isSafeResultHandlerEnabled()).isFalse();
191+
assertThat(config.getDefaultScriptingLanuageInstance()).isInstanceOf(RawLanguageDriver.class);
192+
assertThat(config.isCallSettersOnNulls()).isTrue();
193+
assertThat(config.getLogPrefix()).isEqualTo("mybatis_");
194+
assertThat(config.getLogImpl().getName()).isEqualTo(Slf4jImpl.class.getName());
195+
assertThat(config.getVfsImpl().getName()).isEqualTo(JBoss6VFS.class.getName());
196+
assertThat(config.getConfigurationFactory().getName()).isEqualTo(String.class.getName());
195197

196198
assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("blogauthor").equals(Author.class));
197199
assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("blog").equals(Blog.class));
198200
assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("cart").equals(Cart.class));
199201

200-
assertThat(config.getTypeHandlerRegistry().getTypeHandler(Integer.class), is(instanceOf(CustomIntegerTypeHandler.class)));
201-
assertThat(config.getTypeHandlerRegistry().getTypeHandler(Long.class), is(instanceOf(CustomLongTypeHandler.class)));
202-
assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class), is(instanceOf(CustomStringTypeHandler.class)));
203-
assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class, JdbcType.VARCHAR), is(instanceOf(CustomStringTypeHandler.class)));
204-
assertThat(config.getTypeHandlerRegistry().getTypeHandler(RoundingMode.class), is(instanceOf(EnumOrdinalTypeHandler.class)));
202+
assertThat(config.getTypeHandlerRegistry().getTypeHandler(Integer.class)).isInstanceOf(CustomIntegerTypeHandler.class);
203+
assertThat(config.getTypeHandlerRegistry().getTypeHandler(Long.class)).isInstanceOf(CustomLongTypeHandler.class);
204+
assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class)).isInstanceOf(CustomStringTypeHandler.class);
205+
assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class, JdbcType.VARCHAR)).isInstanceOf(CustomStringTypeHandler.class);
206+
assertThat(config.getTypeHandlerRegistry().getTypeHandler(RoundingMode.class)).isInstanceOf(EnumOrdinalTypeHandler.class);
205207

206208
ExampleObjectFactory objectFactory = (ExampleObjectFactory)config.getObjectFactory();
207-
assertThat(objectFactory.getProperties().size(), is(1));
208-
assertThat(objectFactory.getProperties().getProperty("objectFactoryProperty"), is("100"));
209+
assertThat(objectFactory.getProperties().size()).isEqualTo(1);
210+
assertThat(objectFactory.getProperties().getProperty("objectFactoryProperty")).isEqualTo("100");
209211

210-
assertThat(config.getObjectWrapperFactory(), is(instanceOf(CustomObjectWrapperFactory.class)));
212+
assertThat(config.getObjectWrapperFactory()).isInstanceOf(CustomObjectWrapperFactory.class);
211213

212-
assertThat(config.getReflectorFactory(), is(instanceOf(CustomReflectorFactory.class)));
214+
assertThat(config.getReflectorFactory()).isInstanceOf(CustomReflectorFactory.class);
213215

214216
ExamplePlugin plugin = (ExamplePlugin)config.getInterceptors().get(0);
215-
assertThat(plugin.getProperties().size(), is(1));
216-
assertThat(plugin.getProperties().getProperty("pluginProperty"), is("100"));
217+
assertThat(plugin.getProperties().size()).isEqualTo(1);
218+
assertThat(plugin.getProperties().getProperty("pluginProperty")).isEqualTo("100");
217219

218220
Environment environment = config.getEnvironment();
219-
assertThat(environment.getId(), is("development"));
220-
assertThat(environment.getDataSource(), is(instanceOf(UnpooledDataSource.class)));
221-
assertThat(environment.getTransactionFactory(), is(instanceOf(JdbcTransactionFactory.class)));
221+
assertThat(environment.getId()).isEqualTo("development");
222+
assertThat(environment.getDataSource()).isInstanceOf(UnpooledDataSource.class);
223+
assertThat(environment.getTransactionFactory()).isInstanceOf(JdbcTransactionFactory.class);
222224

223-
assertThat(config.getDatabaseId(), is("derby"));
225+
assertThat(config.getDatabaseId()).isEqualTo("derby");
224226

225-
assertThat(config.getMapperRegistry().getMappers().size(), is(4));
226-
assertThat(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class), is(true));
227-
assertThat(config.getMapperRegistry().hasMapper(CustomMapper.class), is(true));
228-
assertThat(config.getMapperRegistry().hasMapper(BlogMapper.class), is(true));
229-
assertThat(config.getMapperRegistry().hasMapper(NestedBlogMapper.class), is(true));
227+
assertThat(config.getMapperRegistry().getMappers().size()).isEqualTo(4);
228+
assertThat(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class)).isTrue();
229+
assertThat(config.getMapperRegistry().hasMapper(CustomMapper.class)).isTrue();
230+
assertThat(config.getMapperRegistry().hasMapper(BlogMapper.class)).isTrue();
231+
assertThat(config.getMapperRegistry().hasMapper(NestedBlogMapper.class)).isTrue();
230232

231233
}
232234

@@ -236,8 +238,8 @@ public void shouldSuccessfullyLoadXMLConfigFileWithPropertiesUrl() throws Except
236238
InputStream inputStream = Resources.getResourceAsStream(resource);
237239
XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
238240
Configuration config = builder.parse();
239-
assertThat(config.getVariables().get("driver").toString(), is("org.apache.derby.jdbc.EmbeddedDriver"));
240-
assertThat(config.getVariables().get("prop1").toString(), is("bbbb"));
241+
assertThat(config.getVariables().get("driver").toString()).isEqualTo("org.apache.derby.jdbc.EmbeddedDriver");
242+
assertThat(config.getVariables().get("prop1").toString()).isEqualTo("bbbb");
241243

242244
}
243245

0 commit comments

Comments
 (0)