Skip to content

Commit 4a6809c

Browse files
committed
Use AssertJ instead of assertions provided by JUnit
1 parent ae8104c commit 4a6809c

File tree

18 files changed

+209
-220
lines changed

18 files changed

+209
-220
lines changed

md-jee-cdi/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
<groupId>org.junit.jupiter</groupId>
3434
<artifactId>junit-jupiter-engine</artifactId>
3535
</dependency>
36+
37+
<dependency>
38+
<groupId>org.assertj</groupId>
39+
<artifactId>assertj-core</artifactId>
40+
</dependency>
3641
</dependencies>
3742
</project>
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.github.matschieu.jakartaee.cdi;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import org.junit.jupiter.api.AfterAll;
4-
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.BeforeAll;
67
import org.junit.jupiter.api.Test;
78

@@ -13,23 +14,24 @@ class AppLifeCycleTest extends WeldTest {
1314
@BeforeAll
1415
static void beforeContainerStart() {
1516
AppLifeCycleObserverBean.reset();
16-
Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppInitialized());
17-
Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppBeforeDestroyed());
18-
Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppDestroyed());
17+
18+
assertThat(AppLifeCycleObserverBean.getAppInitialized()).isEqualTo(0);
19+
assertThat(AppLifeCycleObserverBean.getAppBeforeDestroyed()).isEqualTo(0);
20+
assertThat(AppLifeCycleObserverBean.getAppDestroyed()).isEqualTo(0);
1921
}
2022

2123
@Test
2224
void testLifeCycleEvent() {
23-
Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppInitialized());
24-
Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppBeforeDestroyed());
25-
Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppDestroyed());
25+
assertThat(AppLifeCycleObserverBean.getAppInitialized()).isEqualTo(1);
26+
assertThat(AppLifeCycleObserverBean.getAppBeforeDestroyed()).isEqualTo(0);
27+
assertThat(AppLifeCycleObserverBean.getAppDestroyed()).isEqualTo(0);
2628
}
2729

2830
@AfterAll
2931
static void afterContainerStop() {
30-
Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppInitialized());
31-
Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppBeforeDestroyed());
32-
Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppDestroyed());
32+
assertThat(AppLifeCycleObserverBean.getAppInitialized()).isEqualTo(1);
33+
assertThat(AppLifeCycleObserverBean.getAppBeforeDestroyed()).isEqualTo(1);
34+
assertThat(AppLifeCycleObserverBean.getAppDestroyed()).isEqualTo(1);
3335
}
3436

3537
}

md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AsynchronousTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.github.matschieu.jakartaee.cdi;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import java.util.concurrent.ExecutionException;
46
import java.util.concurrent.Future;
57

6-
import org.junit.jupiter.api.Assertions;
78
import org.junit.jupiter.api.Test;
89

910
import com.github.matschieu.WeldTest;
@@ -23,11 +24,11 @@ void testAsynchronousBean() throws InterruptedException, ExecutionException {
2324

2425
Thread.sleep(100);
2526

26-
Assertions.assertTrue(future2.isDone());
27-
Assertions.assertFalse(future1.isDone());
27+
assertThat(future2.isDone()).isTrue();
28+
assertThat(future1.isDone()).isFalse();
2829

29-
Assertions.assertTrue(future2.get());
30-
Assertions.assertTrue(future1.get());
30+
assertThat(future2.get()).isTrue();
31+
assertThat(future1.get()).isTrue();
3132
}
3233

3334
}

md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BeanTypeTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.matschieu.jakartaee.cdi;
22

3-
import org.junit.jupiter.api.Assertions;
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
46
import org.junit.jupiter.api.Test;
57

68
import com.github.matschieu.WeldTest;
@@ -29,16 +31,16 @@ class BeanTypeTest extends WeldTest {
2931

3032
@Test
3133
void testInjectionUsingAnnotation() {
32-
Assertions.assertInstanceOf(BookShop.class, bookshop);
33-
Assertions.assertInstanceOf(BookShop.class, business);
34-
Assertions.assertInstanceOf(BookShop.class, shop);
34+
assertThat(bookshop).isInstanceOf(BookShop.class);
35+
assertThat(business).isInstanceOf(BookShop.class);
36+
assertThat(shop).isInstanceOf(BookShop.class);
3537
}
3638

3739
@Test
3840
void testInjectionUsingContainer() {
39-
Assertions.assertInstanceOf(BookShop.class, weld.container().select(BookShop.class).get());
40-
Assertions.assertInstanceOf(BookShop.class, weld.container().select(Business.class).get());
41-
Assertions.assertInstanceOf(BookShop.class, weld.container().select(new TypeLiteral<Shop<Book>>() {}).get());
41+
assertThat(weld.container().select(BookShop.class).get()).isInstanceOf(BookShop.class);
42+
assertThat(weld.container().select(Business.class).get()).isInstanceOf(BookShop.class);
43+
assertThat(weld.container().select(new TypeLiteral<Shop<Book>>() {}).get()).isInstanceOf(BookShop.class);
4244
}
4345

4446
@Inject
@@ -53,9 +55,9 @@ void testInjectionUsingContainer() {
5355
@Test
5456
void testTypedInjectionUsingAnnotation() {
5557
// TypedBookShop has restricted type to TypedShop, it can't be injected as other type
56-
Assertions.assertThrows(Exception.class, () -> typedBookshop.get());
57-
Assertions.assertThrows(Exception.class, () -> typedBusiness.get());
58-
Assertions.assertInstanceOf(TypedBookShop.class, typedShop.get());
58+
assertThatExceptionOfType(Exception.class).isThrownBy(() -> typedBookshop.get());
59+
assertThatExceptionOfType(Exception.class).isThrownBy(() -> typedBusiness.get());
60+
assertThat(typedShop.get()).isInstanceOf(TypedBookShop.class);
5961
}
6062

6163
}

md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BuiltInQualifiersTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.matschieu.jakartaee.cdi;
22

3-
import org.junit.jupiter.api.Assertions;
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
46
import org.junit.jupiter.api.Test;
57

68
import com.github.matschieu.WeldTest;
@@ -60,38 +62,38 @@ class BuiltInQualifiersTest extends WeldTest {
6062
@Test
6163
void testInjectionWithDefaultQualifiersUsingAnnotations() {
6264
// Qualifiers @Any and @Default are implicit and added by default to each bean
63-
Assertions.assertInstanceOf(Bean.class, bean);
64-
Assertions.assertInstanceOf(Bean.class, beanWithDefault);
65-
Assertions.assertInstanceOf(Bean.class, beanWithAnyDefault);
65+
assertThat(bean).isInstanceOf(Bean.class);
66+
assertThat(beanWithDefault).isInstanceOf(Bean.class);
67+
assertThat(beanWithAnyDefault).isInstanceOf(Bean.class);
6668
// @Any and @Default are explicitly declared in the bean but are not necessary to inject it
67-
Assertions.assertInstanceOf(AnyDefaultBean.class, anyDefaultBean);
69+
assertThat(anyDefaultBean).isInstanceOf(AnyDefaultBean.class);
6870
}
6971

7072
@Test
7173
void testInjectionWithDefaultQualifiersUsingProgrammingLookup() {
7274
// Qualifiers @Any and @Default are implicit and added by default to each bean
73-
Assertions.assertInstanceOf(Bean.class, beanInstance.get());
74-
Assertions.assertInstanceOf(Bean.class, beanWithDefaultInstance.get());
75-
Assertions.assertInstanceOf(Bean.class, beanWithAnyDefaultInstance.get());
75+
assertThat(beanInstance.get()).isInstanceOf(Bean.class);
76+
assertThat(beanWithDefaultInstance.get()).isInstanceOf(Bean.class);
77+
assertThat(beanWithAnyDefaultInstance.get()).isInstanceOf(Bean.class);
7678
// @Any and @Default are explicitly declared in the bean but are not necessary to inject it
77-
Assertions.assertInstanceOf(AnyDefaultBean.class, anyDefaultBeanInstance.get());
79+
assertThat(anyDefaultBeanInstance.get()).isInstanceOf(AnyDefaultBean.class);
7880
}
7981

8082
@Test
8183
void testInjectionWithDefaultQualifiersUsingContainer() {
8284
// Qualifiers @Any and @Default are implicit and added by default to each bean
83-
Assertions.assertInstanceOf(Bean.class, weld.container().select(Bean.class).get());
84-
Assertions.assertInstanceOf(Bean.class, weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Default.class)).get());
85-
Assertions.assertInstanceOf(Bean.class, weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Any.class), AnnotationUtils.toAnnotation(Default.class)).get());
85+
assertThat(weld.container().select(Bean.class).get()).isInstanceOf(Bean.class);
86+
assertThat(weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Default.class)).get()).isInstanceOf(Bean.class);
87+
assertThat(weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Any.class), AnnotationUtils.toAnnotation(Default.class)).get()).isInstanceOf(Bean.class);
8688
// @Any and @Default are explicitly declared in the bean but are not necessary to inject it
87-
Assertions.assertInstanceOf(AnyDefaultBean.class, weld.container().select(AnyDefaultBean.class).get());
89+
assertThat(weld.container().select(AnyDefaultBean.class).get()).isInstanceOf(AnyDefaultBean.class);
8890
}
8991

9092
@Test
9193
void testInjectionWithName() {
92-
Assertions.assertInstanceOf(NamedBean.class, namedBean);
94+
assertThat(namedBean).isInstanceOf(NamedBean.class);
9395
// If no name binds with the name defined at the injection point, then an exception is thrown when injecting
94-
Assertions.assertThrows(UnsatisfiedResolutionException.class, () -> badNamedBeanInstance.get());
96+
assertThatExceptionOfType(UnsatisfiedResolutionException.class).isThrownBy(() -> badNamedBeanInstance.get());
9597
}
9698

9799
}

md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/CallbackTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.github.matschieu.jakartaee.cdi;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import org.junit.jupiter.api.AfterAll;
4-
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.BeforeAll;
67
import org.junit.jupiter.api.Test;
78

@@ -24,36 +25,33 @@ class CallbackTest extends WeldTest {
2425
@BeforeAll
2526
static void checkCallbackState() {
2627
// At this step the container is not started, it has created no instance and the counter is 0
27-
Assertions.assertEquals(0, CallbackBean.getAliveInstances());
28+
assertThat(CallbackBean.getAliveInstances()).isZero();
2829
}
2930

3031
@Test
3132
void testPostConstruct() {
3233
// At this step the container has created 3 instances
33-
Assertions.assertEquals(3, CallbackBean.getAliveInstances());
34+
assertThat(CallbackBean.getAliveInstances()).isEqualTo(3);
3435

3536
CallbackBean bean = new CallbackBean();
3637
// A "new" only process the constructor, not the @PostConstruct method managed by the container
3738
// So instance number is not initialized and the counter of alive instance is not incremented
38-
Assertions.assertNull(bean.getInstanceNumber());
39-
Assertions.assertEquals(3, CallbackBean.getAliveInstances());
39+
assertThat(bean.getInstanceNumber()).isNull();
40+
assertThat(CallbackBean.getAliveInstances()).isEqualTo(3);
4041

4142
// The container manage the bean and process the @PostConstruct method (after the constructor)
4243
// so each injected bean has an instance number (injection are done in the order of field declarations)
43-
Assertions.assertNotNull(bean1.getInstanceNumber());
44-
Assertions.assertNotNull(bean2.getInstanceNumber());
45-
Assertions.assertNotNull(bean3.getInstanceNumber());
46-
Assertions.assertEquals(0, bean1.getInstanceNumber());
47-
Assertions.assertEquals(1, bean2.getInstanceNumber());
48-
Assertions.assertEquals(2, bean3.getInstanceNumber());
44+
assertThat(bean1.getInstanceNumber()).isNotNull().isEqualTo(0);
45+
assertThat(bean2.getInstanceNumber()).isNotNull().isEqualTo(1);
46+
assertThat(bean3.getInstanceNumber()).isNotNull().isEqualTo(2);
4947
}
5048

5149
@AfterAll
5250
static void validatePreDestroy() {
5351
// Her is the proof that PreDestroy method has been called when the cointainer was stopped
5452
// Each predestroy has decreased the counter of alive instances
5553
// The container has created 3 instances and destroyed the sames
56-
Assertions.assertEquals(0, CallbackBean.getAliveInstances());
54+
assertThat(CallbackBean.getAliveInstances()).isZero();
5755
}
5856

5957
}

0 commit comments

Comments
 (0)