From 4a6809cd54896444cd390381a4965217082fd3ee Mon Sep 17 00:00:00 2001 From: Matschieu Date: Tue, 24 Dec 2024 15:33:41 +0100 Subject: [PATCH] Use AssertJ instead of assertions provided by JUnit --- md-jee-cdi/pom.xml | 5 + .../jakartaee/cdi/AppLifeCycleTest.java | 22 ++-- .../jakartaee/cdi/AsynchronousTest.java | 11 +- .../matschieu/jakartaee/cdi/BeanTypeTest.java | 22 ++-- .../jakartaee/cdi/BuiltInQualifiersTest.java | 32 +++--- .../matschieu/jakartaee/cdi/CallbackTest.java | 22 ++-- .../jakartaee/cdi/InjectionTest.java | 106 +++++++++--------- .../jakartaee/cdi/InterceptorTest.java | 6 +- .../matschieu/jakartaee/cdi/ProducerTest.java | 31 ++--- .../jakartaee/cdi/QualifierTest.java | 11 +- .../matschieu/jakartaee/cdi/ScopeTest.java | 15 +-- .../jakartaee/cdi/SpecializationTest.java | 11 +- md-jee-interceptor/pom.xml | 5 + .../jee/interceptor/dummy/MyServiceTest.java | 12 +- .../validation/ResourceServiceTest.java | 76 +++---------- md-jee-validation/pom.xml | 5 + .../jakartaee/validation/BookTest.java | 18 +-- .../jakartaee/validation/UserTest.java | 19 ++-- 18 files changed, 209 insertions(+), 220 deletions(-) diff --git a/md-jee-cdi/pom.xml b/md-jee-cdi/pom.xml index 4016be8..e46fb18 100644 --- a/md-jee-cdi/pom.xml +++ b/md-jee-cdi/pom.xml @@ -33,5 +33,10 @@ org.junit.jupiter junit-jupiter-engine + + + org.assertj + assertj-core + \ No newline at end of file diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AppLifeCycleTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AppLifeCycleTest.java index 955bcaa..62c9643 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AppLifeCycleTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AppLifeCycleTest.java @@ -1,7 +1,8 @@ package com.github.matschieu.jakartaee.cdi; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -13,23 +14,24 @@ class AppLifeCycleTest extends WeldTest { @BeforeAll static void beforeContainerStart() { AppLifeCycleObserverBean.reset(); - Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppInitialized()); - Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppBeforeDestroyed()); - Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppDestroyed()); + + assertThat(AppLifeCycleObserverBean.getAppInitialized()).isEqualTo(0); + assertThat(AppLifeCycleObserverBean.getAppBeforeDestroyed()).isEqualTo(0); + assertThat(AppLifeCycleObserverBean.getAppDestroyed()).isEqualTo(0); } @Test void testLifeCycleEvent() { - Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppInitialized()); - Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppBeforeDestroyed()); - Assertions.assertEquals(0, AppLifeCycleObserverBean.getAppDestroyed()); + assertThat(AppLifeCycleObserverBean.getAppInitialized()).isEqualTo(1); + assertThat(AppLifeCycleObserverBean.getAppBeforeDestroyed()).isEqualTo(0); + assertThat(AppLifeCycleObserverBean.getAppDestroyed()).isEqualTo(0); } @AfterAll static void afterContainerStop() { - Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppInitialized()); - Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppBeforeDestroyed()); - Assertions.assertEquals(1, AppLifeCycleObserverBean.getAppDestroyed()); + assertThat(AppLifeCycleObserverBean.getAppInitialized()).isEqualTo(1); + assertThat(AppLifeCycleObserverBean.getAppBeforeDestroyed()).isEqualTo(1); + assertThat(AppLifeCycleObserverBean.getAppDestroyed()).isEqualTo(1); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AsynchronousTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AsynchronousTest.java index 787d815..4940a19 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AsynchronousTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/AsynchronousTest.java @@ -1,9 +1,10 @@ package com.github.matschieu.jakartaee.cdi; +import static org.assertj.core.api.Assertions.assertThat; + import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -23,11 +24,11 @@ void testAsynchronousBean() throws InterruptedException, ExecutionException { Thread.sleep(100); - Assertions.assertTrue(future2.isDone()); - Assertions.assertFalse(future1.isDone()); + assertThat(future2.isDone()).isTrue(); + assertThat(future1.isDone()).isFalse(); - Assertions.assertTrue(future2.get()); - Assertions.assertTrue(future1.get()); + assertThat(future2.get()).isTrue(); + assertThat(future1.get()).isTrue(); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BeanTypeTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BeanTypeTest.java index 311b343..9d5864f 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BeanTypeTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BeanTypeTest.java @@ -1,6 +1,8 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -29,16 +31,16 @@ class BeanTypeTest extends WeldTest { @Test void testInjectionUsingAnnotation() { - Assertions.assertInstanceOf(BookShop.class, bookshop); - Assertions.assertInstanceOf(BookShop.class, business); - Assertions.assertInstanceOf(BookShop.class, shop); + assertThat(bookshop).isInstanceOf(BookShop.class); + assertThat(business).isInstanceOf(BookShop.class); + assertThat(shop).isInstanceOf(BookShop.class); } @Test void testInjectionUsingContainer() { - Assertions.assertInstanceOf(BookShop.class, weld.container().select(BookShop.class).get()); - Assertions.assertInstanceOf(BookShop.class, weld.container().select(Business.class).get()); - Assertions.assertInstanceOf(BookShop.class, weld.container().select(new TypeLiteral>() {}).get()); + assertThat(weld.container().select(BookShop.class).get()).isInstanceOf(BookShop.class); + assertThat(weld.container().select(Business.class).get()).isInstanceOf(BookShop.class); + assertThat(weld.container().select(new TypeLiteral>() {}).get()).isInstanceOf(BookShop.class); } @Inject @@ -53,9 +55,9 @@ void testInjectionUsingContainer() { @Test void testTypedInjectionUsingAnnotation() { // TypedBookShop has restricted type to TypedShop, it can't be injected as other type - Assertions.assertThrows(Exception.class, () -> typedBookshop.get()); - Assertions.assertThrows(Exception.class, () -> typedBusiness.get()); - Assertions.assertInstanceOf(TypedBookShop.class, typedShop.get()); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> typedBookshop.get()); + assertThatExceptionOfType(Exception.class).isThrownBy(() -> typedBusiness.get()); + assertThat(typedShop.get()).isInstanceOf(TypedBookShop.class); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BuiltInQualifiersTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BuiltInQualifiersTest.java index 14cfaa8..f389cd6 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BuiltInQualifiersTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/BuiltInQualifiersTest.java @@ -1,6 +1,8 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -60,38 +62,38 @@ class BuiltInQualifiersTest extends WeldTest { @Test void testInjectionWithDefaultQualifiersUsingAnnotations() { // Qualifiers @Any and @Default are implicit and added by default to each bean - Assertions.assertInstanceOf(Bean.class, bean); - Assertions.assertInstanceOf(Bean.class, beanWithDefault); - Assertions.assertInstanceOf(Bean.class, beanWithAnyDefault); + assertThat(bean).isInstanceOf(Bean.class); + assertThat(beanWithDefault).isInstanceOf(Bean.class); + assertThat(beanWithAnyDefault).isInstanceOf(Bean.class); // @Any and @Default are explicitly declared in the bean but are not necessary to inject it - Assertions.assertInstanceOf(AnyDefaultBean.class, anyDefaultBean); + assertThat(anyDefaultBean).isInstanceOf(AnyDefaultBean.class); } @Test void testInjectionWithDefaultQualifiersUsingProgrammingLookup() { // Qualifiers @Any and @Default are implicit and added by default to each bean - Assertions.assertInstanceOf(Bean.class, beanInstance.get()); - Assertions.assertInstanceOf(Bean.class, beanWithDefaultInstance.get()); - Assertions.assertInstanceOf(Bean.class, beanWithAnyDefaultInstance.get()); + assertThat(beanInstance.get()).isInstanceOf(Bean.class); + assertThat(beanWithDefaultInstance.get()).isInstanceOf(Bean.class); + assertThat(beanWithAnyDefaultInstance.get()).isInstanceOf(Bean.class); // @Any and @Default are explicitly declared in the bean but are not necessary to inject it - Assertions.assertInstanceOf(AnyDefaultBean.class, anyDefaultBeanInstance.get()); + assertThat(anyDefaultBeanInstance.get()).isInstanceOf(AnyDefaultBean.class); } @Test void testInjectionWithDefaultQualifiersUsingContainer() { // Qualifiers @Any and @Default are implicit and added by default to each bean - Assertions.assertInstanceOf(Bean.class, weld.container().select(Bean.class).get()); - Assertions.assertInstanceOf(Bean.class, weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Default.class)).get()); - Assertions.assertInstanceOf(Bean.class, weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Any.class), AnnotationUtils.toAnnotation(Default.class)).get()); + assertThat(weld.container().select(Bean.class).get()).isInstanceOf(Bean.class); + assertThat(weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Default.class)).get()).isInstanceOf(Bean.class); + assertThat(weld.container().select(Bean.class, AnnotationUtils.toAnnotation(Any.class), AnnotationUtils.toAnnotation(Default.class)).get()).isInstanceOf(Bean.class); // @Any and @Default are explicitly declared in the bean but are not necessary to inject it - Assertions.assertInstanceOf(AnyDefaultBean.class, weld.container().select(AnyDefaultBean.class).get()); + assertThat(weld.container().select(AnyDefaultBean.class).get()).isInstanceOf(AnyDefaultBean.class); } @Test void testInjectionWithName() { - Assertions.assertInstanceOf(NamedBean.class, namedBean); + assertThat(namedBean).isInstanceOf(NamedBean.class); // If no name binds with the name defined at the injection point, then an exception is thrown when injecting - Assertions.assertThrows(UnsatisfiedResolutionException.class, () -> badNamedBeanInstance.get()); + assertThatExceptionOfType(UnsatisfiedResolutionException.class).isThrownBy(() -> badNamedBeanInstance.get()); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/CallbackTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/CallbackTest.java index 963daf1..bfb019e 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/CallbackTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/CallbackTest.java @@ -1,7 +1,8 @@ package com.github.matschieu.jakartaee.cdi; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -24,28 +25,25 @@ class CallbackTest extends WeldTest { @BeforeAll static void checkCallbackState() { // At this step the container is not started, it has created no instance and the counter is 0 - Assertions.assertEquals(0, CallbackBean.getAliveInstances()); + assertThat(CallbackBean.getAliveInstances()).isZero(); } @Test void testPostConstruct() { // At this step the container has created 3 instances - Assertions.assertEquals(3, CallbackBean.getAliveInstances()); + assertThat(CallbackBean.getAliveInstances()).isEqualTo(3); CallbackBean bean = new CallbackBean(); // A "new" only process the constructor, not the @PostConstruct method managed by the container // So instance number is not initialized and the counter of alive instance is not incremented - Assertions.assertNull(bean.getInstanceNumber()); - Assertions.assertEquals(3, CallbackBean.getAliveInstances()); + assertThat(bean.getInstanceNumber()).isNull(); + assertThat(CallbackBean.getAliveInstances()).isEqualTo(3); // The container manage the bean and process the @PostConstruct method (after the constructor) // so each injected bean has an instance number (injection are done in the order of field declarations) - Assertions.assertNotNull(bean1.getInstanceNumber()); - Assertions.assertNotNull(bean2.getInstanceNumber()); - Assertions.assertNotNull(bean3.getInstanceNumber()); - Assertions.assertEquals(0, bean1.getInstanceNumber()); - Assertions.assertEquals(1, bean2.getInstanceNumber()); - Assertions.assertEquals(2, bean3.getInstanceNumber()); + assertThat(bean1.getInstanceNumber()).isNotNull().isEqualTo(0); + assertThat(bean2.getInstanceNumber()).isNotNull().isEqualTo(1); + assertThat(bean3.getInstanceNumber()).isNotNull().isEqualTo(2); } @AfterAll @@ -53,7 +51,7 @@ static void validatePreDestroy() { // Her is the proof that PreDestroy method has been called when the cointainer was stopped // Each predestroy has decreased the counter of alive instances // The container has created 3 instances and destroyed the sames - Assertions.assertEquals(0, CallbackBean.getAliveInstances()); + assertThat(CallbackBean.getAliveInstances()).isZero(); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InjectionTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InjectionTest.java index 5e19321..91f20ee 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InjectionTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InjectionTest.java @@ -1,5 +1,8 @@ package com.github.matschieu.jakartaee.cdi; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + import java.util.ArrayList; import java.util.Arrays; import java.util.Map; @@ -7,7 +10,6 @@ import org.jboss.weld.environment.se.Weld; import org.jboss.weld.environment.se.WeldContainer; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -62,26 +64,26 @@ class InjectionTest extends WeldTest { @Test void testInjectionUsingAnnotation() { - Assertions.assertInstanceOf(Bean.class, bean1); - Assertions.assertInstanceOf(Bean.class, bean2); + assertThat(bean1).isInstanceOf(Bean.class); + assertThat(bean2).isInstanceOf(Bean.class); // Each injection of a bean create a new instance - Assertions.assertNotEquals(bean1, bean2); - Assertions.assertNotSame(bean1, bean2); + assertThat(bean1).isNotEqualTo(bean2); + assertThat(bean1).isNotSameAs(bean2); } @Test void testInjectionUsingProgrammingLookup() { - Assertions.assertTrue(bean1Instance.isResolvable()); - Assertions.assertTrue(bean2Instance.isResolvable()); + assertThat(bean1Instance.isResolvable()).isTrue(); + assertThat(bean2Instance.isResolvable()).isTrue(); final Bean bean1 = bean1Instance.get(); final Bean bean2 = bean2Instance.get(); - Assertions.assertInstanceOf(Bean.class, bean1); - Assertions.assertInstanceOf(Bean.class, bean2); + assertThat(bean1).isInstanceOf(Bean.class); + assertThat(bean2).isInstanceOf(Bean.class); // Each injection of a bean create a new instance - Assertions.assertNotEquals(bean1, bean2); - Assertions.assertNotSame(bean1, bean2); + assertThat(bean1).isNotEqualTo(bean2); + assertThat(bean1).isNotSameAs(bean2); } @Test @@ -89,76 +91,76 @@ void testInjectionUsingContainer() { final Instance bean1Instance = weld.container().select(Bean.class); final Instance bean2Instance = weld.container().select(Bean.class); - Assertions.assertTrue(bean1Instance.isResolvable()); - Assertions.assertTrue(bean2Instance.isResolvable()); + assertThat(bean1Instance.isResolvable()).isTrue(); + assertThat(bean2Instance.isResolvable()).isTrue(); final Bean bean1 = bean1Instance.get(); final Bean bean2 = bean2Instance.get(); - Assertions.assertInstanceOf(Bean.class, bean1); - Assertions.assertInstanceOf(Bean.class, bean2); + assertThat(bean1).isInstanceOf(Bean.class); + assertThat(bean2).isInstanceOf(Bean.class); // Each injection of a bean create a new instance - Assertions.assertNotEquals(bean1, bean2); - Assertions.assertNotSame(bean1, bean2); + assertThat(bean1).isNotEqualTo(bean2); + assertThat(bean1).isNotSameAs(bean2); } @Test void testSingletonInjection() { - Assertions.assertInstanceOf(SingletonBean.class, singletonBean1); - Assertions.assertInstanceOf(SingletonBean.class, singletonBean2); + assertThat(singletonBean1).isInstanceOf(SingletonBean.class); + assertThat(singletonBean2).isInstanceOf(SingletonBean.class); // Because a Singleton has only one instance, the container inject the same instance of a Singleton - Assertions.assertEquals(singletonBean1, singletonBean2); - Assertions.assertSame(singletonBean1, singletonBean2); + assertThat(singletonBean1).isEqualTo(singletonBean2); + assertThat(singletonBean1).isSameAs(singletonBean2); } @Test void AmbigousDependenciesTest() { // Ambigous because object is the superclass of everything - Assertions.assertFalse(ambigousBean.isResolvable()); - Assertions.assertTrue(ambigousBean.isAmbiguous()); + assertThat(ambigousBean.isResolvable()).isFalse(); + assertThat(ambigousBean.isAmbiguous()).isTrue(); } @Test void unsatisfiedDependenciesTest() { // Unsatisfied because no bean has these both qualifiers - Assertions.assertFalse(weld.container().select(AnnotationUtils.toAnnotation(Asynchronous.class), AnnotationUtils.toAnnotation(Reliable.class)).isResolvable()); - Assertions.assertTrue(weld.container().select(AnnotationUtils.toAnnotation(Asynchronous.class), AnnotationUtils.toAnnotation(Reliable.class)).isUnsatisfied()); + assertThat(weld.container().select(AnnotationUtils.toAnnotation(Asynchronous.class), AnnotationUtils.toAnnotation(Reliable.class)).isResolvable()).isFalse(); + assertThat(weld.container().select(AnnotationUtils.toAnnotation(Asynchronous.class), AnnotationUtils.toAnnotation(Reliable.class)).isUnsatisfied()).isTrue(); } @Test void testVetoedPackage() { - Assertions.assertFalse(CDI.current().select(VetoedClassA.class).isResolvable()); - Assertions.assertFalse(CDI.current().select(VetoedClassB.class).isResolvable()); - Assertions.assertFalse(CDI.current().select(VetoedClassC.class).isResolvable()); + assertThat(CDI.current().select(VetoedClassA.class).isResolvable()).isFalse(); + assertThat(CDI.current().select(VetoedClassB.class).isResolvable()).isFalse(); + assertThat(CDI.current().select(VetoedClassC.class).isResolvable()).isFalse(); - Assertions.assertTrue(CDI.current().select(VetoedClassA.class).isUnsatisfied()); - Assertions.assertTrue(CDI.current().select(VetoedClassB.class).isUnsatisfied()); - Assertions.assertTrue(CDI.current().select(VetoedClassC.class).isUnsatisfied()); + assertThat(CDI.current().select(VetoedClassA.class).isUnsatisfied()).isTrue(); + assertThat(CDI.current().select(VetoedClassB.class).isUnsatisfied()).isTrue(); + assertThat(CDI.current().select(VetoedClassC.class).isUnsatisfied()).isTrue(); - Assertions.assertThrows(UnsatisfiedResolutionException.class, () -> CDI.current().select(VetoedClassA.class).get()); - Assertions.assertThrows(UnsatisfiedResolutionException.class, () -> CDI.current().select(VetoedClassB.class).get()); - Assertions.assertThrows(UnsatisfiedResolutionException.class, () -> CDI.current().select(VetoedClassC.class).get()); + assertThatExceptionOfType(UnsatisfiedResolutionException.class).isThrownBy(() -> CDI.current().select(VetoedClassA.class).get()); + assertThatExceptionOfType(UnsatisfiedResolutionException.class).isThrownBy(() -> CDI.current().select(VetoedClassB.class).get()); + assertThatExceptionOfType(UnsatisfiedResolutionException.class).isThrownBy(() -> CDI.current().select(VetoedClassC.class).get()); } @Test void testVetoedClass() { // This injection is not ambigous due to the use of the Vetoed which exclude the package from the bean management by the container - Assertions.assertInstanceOf(NotVetoedBean.class, notVetoedBean); - Assertions.assertFalse(vetoedBean.isResolvable()); + assertThat(notVetoedBean).isInstanceOf(NotVetoedBean.class); + assertThat(vetoedBean.isResolvable()).isFalse(); // Getting an exception when trying to inject a Vetoed bean - Assertions.assertThrows(UnsatisfiedResolutionException.class, () -> vetoedBean.get()); + assertThatExceptionOfType(UnsatisfiedResolutionException.class).isThrownBy(() -> vetoedBean.get()); } @Test void testInitializers() { // Default constructor is not called as there is another constructor annotated @Inject - Assertions.assertNull(initBean.getDefaultConstructorBean()); + assertThat(initBean.getDefaultConstructorBean()).isNull(); - Assertions.assertNotNull(initBean.getConstructorBean()); - Assertions.assertNotNull(initBean.getField1Bean()); - Assertions.assertNotNull(initBean.getField2Bean()); - Assertions.assertNotNull(initBean.getMethod1Bean()); - Assertions.assertNotNull(initBean.getMethod2Bean()); + assertThat(initBean.getConstructorBean()).isNotNull(); + assertThat(initBean.getField1Bean()).isNotNull(); + assertThat(initBean.getField2Bean()).isNotNull(); + assertThat(initBean.getMethod1Bean()).isNotNull(); + assertThat(initBean.getMethod2Bean()).isNotNull(); final Map injectionOrder = new TreeMap<>(); injectionOrder.put(initBean.getMethod2Bean().getInstanceNumber(), "METHOD2"); @@ -173,12 +175,12 @@ void testInitializers() { // Order of injection of fields or methods by the container is not guaranteed // But it is always starts with the constructor, then the fields and at the ends the methods - Assertions.assertEquals(5, values.size()); - Assertions.assertEquals("CONSTRUCTOR", values.get(0)); - Assertions.assertEquals("FIELD", values.get(1).substring(0, values.get(1).length() - 1)); - Assertions.assertEquals("FIELD", values.get(2).substring(0, values.get(2).length() - 1)); - Assertions.assertEquals("METHOD", values.get(3).substring(0, values.get(3).length() - 1)); - Assertions.assertEquals("METHOD", values.get(4).substring(0, values.get(4).length() - 1)); + assertThat(values.size()).isEqualTo(5); + assertThat(values.get(0)).isEqualTo("CONSTRUCTOR"); + assertThat(values.get(1).substring(0, values.get(1).length() - 1)).isEqualTo("FIELD"); + assertThat(values.get(2).substring(0, values.get(2).length() - 1)).isEqualTo("FIELD"); + assertThat(values.get(3).substring(0, values.get(3).length() - 1)).isEqualTo("METHOD"); + assertThat(values.get(4).substring(0, values.get(4).length() - 1)).isEqualTo("METHOD"); } @Test @@ -190,10 +192,10 @@ void testMultiContainer() { final SingletonBean singletonBean2 = container2.select(SingletonBean.class).get(); // A singleton is a unique instance, so 2 instances of a singleton inside the same container are equals - Assertions.assertSame(singletonBean1, container1.select(SingletonBean.class).get()); - Assertions.assertSame(singletonBean2, container2.select(SingletonBean.class).get()); + assertThat(singletonBean1).isSameAs(container1.select(SingletonBean.class).get()); + assertThat(singletonBean2).isSameAs(container2.select(SingletonBean.class).get()); // But 2 instances of a singleton from both container are not equals - Assertions.assertNotSame(singletonBean1, singletonBean2); + assertThat(singletonBean1).isNotSameAs(singletonBean2); container1.shutdown(); container2.shutdown(); diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InterceptorTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InterceptorTest.java index bb8652d..385aa95 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InterceptorTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/InterceptorTest.java @@ -1,6 +1,7 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -15,6 +16,7 @@ class InterceptorTest extends WeldTest { @Test void testInterceptor() { - Assertions.assertEquals("raBooF", bean.reverseString("FooBar")); + assertThat(bean.reverseString("FooBar")).isEqualTo("raBooF"); } + } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ProducerTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ProducerTest.java index acfc5a3..62a27fa 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ProducerTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ProducerTest.java @@ -1,6 +1,7 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -48,44 +49,44 @@ class ProducerTest extends WeldTest { @Test void testPrimitiveProducer() { // Use the producer to get a random integer - Assertions.assertNotNull(randomInteger); - Assertions.assertNotEquals(0, randomInteger.intValue()); + assertThat(randomInteger).isNotNull(); + assertThat(randomInteger.intValue()).isNotZero(); // Use the producer to get a random integer converted to int by autoboxing - Assertions.assertNotEquals(0, randomInt); - Assertions.assertNull(nullInteger); + assertThat(randomInt).isNotZero(); + assertThat(nullInteger).isNull(); // The producer returns a null so the container inject the primitive type’s default value to avoid NPE - Assertions.assertEquals(0, nullInt); + assertThat(nullInt).isZero(); // The producer can simply return a primitive type - Assertions.assertNotEquals(0.0, randomDouble); + assertThat(randomDouble).isNotEqualTo(0.0); } @Test void testProducer() { PaymentProcessorBuilder.setSynchronous(true); - Assertions.assertInstanceOf(SynchronousPaymentProcessor.class, paymentProcessor.get()); + assertThat(paymentProcessor.get()).isInstanceOf(SynchronousPaymentProcessor.class); PaymentProcessorBuilder.setSynchronous(false); - Assertions.assertInstanceOf(AsynchronousPaymentProcessor.class, paymentProcessor.get()); + assertThat(paymentProcessor.get()).isInstanceOf(AsynchronousPaymentProcessor.class); } @Test void testDisposer() { // When instanciated outside the container, producer and disposer are not called ProducedBean localBean = new ProducedBean(); - Assertions.assertFalse(localBean.isProduced()); - Assertions.assertFalse(localBean.isDisposed()); + assertThat(localBean.isProduced()).isFalse(); + assertThat(localBean.isDisposed()).isFalse(); producedBeanInstance.destroy(localBean); // The producer is called when injecting the bean ProducedBean bean = producedBeanInstance.get(); - Assertions.assertNotNull(bean); - Assertions.assertTrue(bean.isProduced()); - Assertions.assertFalse(bean.isDisposed()); + assertThat(bean).isNotNull(); + assertThat(bean.isProduced()).isTrue(); + assertThat(bean.isDisposed()).isFalse(); // The container destroy the bean, the disposer is called producedBeanInstance.destroy(bean); - Assertions.assertTrue(bean.isDisposed()); + assertThat(bean.isDisposed()).isTrue(); // Do nothing, the bean has already been destroyed by the container // The instance of bean is still existing in memory but is not managed anymore by the container diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/QualifierTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/QualifierTest.java index c757622..4b93ebb 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/QualifierTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/QualifierTest.java @@ -1,6 +1,7 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -34,10 +35,10 @@ class QualifierTest extends WeldTest { @Test void testInjectionWithQualifier() { - Assertions.assertInstanceOf(SynchronousPaymentProcessor.class, syncPaymentProcessor); - Assertions.assertInstanceOf(AsynchronousPaymentProcessor.class, asyncPaymentProcessor); - Assertions.assertInstanceOf(SynchronousPaymentProcessor.class, reliablePaymentProcessor); - Assertions.assertInstanceOf(SynchronousPaymentProcessor.class, reliableSynchronousPaymentProcessor); + assertThat(syncPaymentProcessor).isInstanceOf(SynchronousPaymentProcessor.class); + assertThat(asyncPaymentProcessor).isInstanceOf(AsynchronousPaymentProcessor.class); + assertThat(reliablePaymentProcessor).isInstanceOf(SynchronousPaymentProcessor.class); + assertThat(reliableSynchronousPaymentProcessor).isInstanceOf(SynchronousPaymentProcessor.class); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ScopeTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ScopeTest.java index 038e2c2..c85e13a 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ScopeTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/ScopeTest.java @@ -1,6 +1,7 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -25,19 +26,19 @@ class ScopeTest extends WeldTest { @Test void testDependentScoped() { - Assertions.assertNotNull(dependentBean1); - Assertions.assertNotNull(dependentBean2); + assertThat(dependentBean1).isNotNull(); + assertThat(dependentBean2).isNotNull(); // dependent is the scope by default for any bean // When injected, a new instance of the dependent beane is created - Assertions.assertNotEquals(dependentBean1, dependentBean2); + assertThat(dependentBean1).isNotEqualTo(dependentBean2); } @Test void testApplicationScoped() { - Assertions.assertNotNull(appBean1); - Assertions.assertNotNull(appBean2); + assertThat(appBean1).isNotNull(); + assertThat(appBean2).isNotNull(); // ApplicationScoped beans are instanciated once for the whole application - Assertions.assertEquals(appBean1, appBean2); + assertThat(appBean1).isEqualTo(appBean2); } } diff --git a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/SpecializationTest.java b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/SpecializationTest.java index d90d6e7..a449c88 100644 --- a/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/SpecializationTest.java +++ b/md-jee-cdi/src/test/java/com/github/matschieu/jakartaee/cdi/SpecializationTest.java @@ -1,6 +1,7 @@ package com.github.matschieu.jakartaee.cdi; -import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import com.github.matschieu.WeldTest; @@ -32,12 +33,12 @@ class SpecializationTest extends WeldTest { @Test void testAlternative() { // MockAsynchronous service because priority increased on the alternative with @Priority - Assertions.assertInstanceOf(MockAsynchronousService.class, service); + assertThat(service).isInstanceOf(MockAsynchronousService.class); // AsynchronousService because qualifier @Asynchronous is only on the Default implementation - Assertions.assertInstanceOf(AsynchronousService.class, asyncService); + assertThat(asyncService).isInstanceOf(AsynchronousService.class); // Both SpecializedService have the same type because of the use of the annotation @Specializes // @Specializes guarantees that the second bean is never instantiated or called by the container, even if the second bean defines a producer or observer method - Assertions.assertInstanceOf(MockAsynchronousSpecializedService.class, specializedService); - Assertions.assertInstanceOf(MockAsynchronousSpecializedService.class, asyncSpecializedService); + assertThat(specializedService).isInstanceOf(MockAsynchronousSpecializedService.class); + assertThat(asyncSpecializedService).isInstanceOf(MockAsynchronousSpecializedService.class); } } diff --git a/md-jee-interceptor/pom.xml b/md-jee-interceptor/pom.xml index 23d9214..8291812 100644 --- a/md-jee-interceptor/pom.xml +++ b/md-jee-interceptor/pom.xml @@ -33,5 +33,10 @@ org.junit.jupiter junit-jupiter-engine + + + org.assertj + assertj-core + \ No newline at end of file diff --git a/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/dummy/MyServiceTest.java b/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/dummy/MyServiceTest.java index a8e4051..bca9294 100644 --- a/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/dummy/MyServiceTest.java +++ b/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/dummy/MyServiceTest.java @@ -1,9 +1,10 @@ package com.github.matschieu.jee.interceptor.dummy; +import static org.assertj.core.api.Assertions.assertThat; + import org.jboss.weld.junit5.EnableWeld; import org.jboss.weld.junit5.WeldInitiator; import org.jboss.weld.junit5.WeldSetup; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import jakarta.inject.Inject; @@ -19,9 +20,10 @@ class MyServiceTest { @Test void testMyService() { - Assertions.assertEquals(2, this.myService.getInt()); - Assertions.assertEquals(Integer.valueOf(2), this.myService.getInteger()); - Assertions.assertEquals("{[MyString]}", this.myService.getString()); - Assertions.assertEquals("{[MyOtherString]}", this.myService.getOtherString()); + assertThat(this.myService.getInt()).isEqualTo(2); + assertThat(this.myService.getInteger()).isEqualTo(Integer.valueOf(2)); + assertThat(this.myService.getString()).isEqualTo("{[MyString]}"); + assertThat(this.myService.getOtherString()).isEqualTo("{[MyOtherString]}"); } + } diff --git a/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/validation/ResourceServiceTest.java b/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/validation/ResourceServiceTest.java index deb01bf..87e25c1 100644 --- a/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/validation/ResourceServiceTest.java +++ b/md-jee-interceptor/src/test/java/com/github/matschieu/jee/interceptor/validation/ResourceServiceTest.java @@ -1,11 +1,12 @@ package com.github.matschieu.jee.interceptor.validation; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatNoException; import org.jboss.weld.junit5.EnableWeld; import org.jboss.weld.junit5.WeldInitiator; import org.jboss.weld.junit5.WeldSetup; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import jakarta.inject.Inject; @@ -21,77 +22,34 @@ class ResourceServiceTest { @Test void testMyServiceCreateResource1() { - try { - this.resourceService.createResource(null, "", ""); - fail(); - } catch (final Exception e) { - Assertions.assertEquals(NullElementException.class, e.getClass()); - Assertions.assertEquals("arg0 is null", e.getMessage()); - } - - try { - this.resourceService.createResource("", null, ""); - fail(); - } catch (final Exception e) { - Assertions.assertEquals(NullElementException.class, e.getClass()); - Assertions.assertEquals("arg1 is null", e.getMessage()); - } - - this.resourceService.createResource("", "", null); + assertThatExceptionOfType(NullElementException.class).isThrownBy(() -> this.resourceService.createResource(null, "", "")).withMessage("arg0 is null"); + assertThatExceptionOfType(NullElementException.class).isThrownBy(() -> this.resourceService.createResource("", null, "")).withMessage("arg1 is null"); + assertThatNoException().isThrownBy(() -> this.resourceService.createResource("", "", null)); } @Test void testMyServiceCreateResource2() { - try { - this.resourceService.createResource(new Resource(null, "", "")); - fail(); - } catch (final Exception e) { - Assertions.assertEquals(NullElementException.class, e.getClass()); - Assertions.assertEquals("name is null", e.getMessage()); - } - - try { - this.resourceService.createResource(new Resource("", null, "")); - fail(); - } catch (final Exception e) { - Assertions.assertEquals(NullElementException.class, e.getClass()); - Assertions.assertEquals("type is null", e.getMessage()); - } - - this.resourceService.createResource(new Resource("", "", null)); + assertThatExceptionOfType(NullElementException.class).isThrownBy(() -> this.resourceService.createResource(new Resource(null, "", ""))).withMessage("name is null"); + assertThatExceptionOfType(NullElementException.class).isThrownBy(() -> this.resourceService.createResource(new Resource("", null, ""))).withMessage("type is null"); + assertThatNoException().isThrownBy(() -> this.resourceService.createResource("", "", null)); } @Test void testMyServiceGetResource() { - Resource resource; - - try { - resource = this.resourceService.getResource(null); - fail(); - } catch (final Exception e) { - Assertions.assertEquals(NullElementException.class, e.getClass()); - Assertions.assertEquals("arg0 is null", e.getMessage()); - } + assertThatExceptionOfType(NullElementException.class).isThrownBy(() -> this.resourceService.getResource(null)).withMessage("arg0 is null"); - resource = this.resourceService.getResource("name"); + Resource resource = this.resourceService.getResource("name"); - Assertions.assertNotNull(resource); - Assertions.assertEquals("name", resource.getName()); - Assertions.assertEquals("type", resource.getType()); - Assertions.assertEquals("description", resource.getDescription()); + assertThat(resource).isNotNull(); + assertThat(resource.getName()).isEqualTo("name"); + assertThat(resource.getType()).isEqualTo("type"); + assertThat(resource.getDescription()).isEqualTo("description"); } @Test void testMyServiceDeleteResource() { - try { - this.resourceService.deleteResource(null); - fail(); - } catch (final Exception e) { - Assertions.assertEquals(NullElementException.class, e.getClass()); - Assertions.assertEquals("arg0 is null", e.getMessage()); - } - - this.resourceService.getResource("name"); + assertThatExceptionOfType(NullElementException.class).isThrownBy(() -> this.resourceService.deleteResource(null)).withMessage("arg0 is null"); + assertThatNoException().isThrownBy(() -> this.resourceService.getResource("name")); } } diff --git a/md-jee-validation/pom.xml b/md-jee-validation/pom.xml index a9aa2a2..417f9a9 100644 --- a/md-jee-validation/pom.xml +++ b/md-jee-validation/pom.xml @@ -24,6 +24,11 @@ junit-jupiter-engine + + org.assertj + assertj-core + + org.hibernate.validator hibernate-validator diff --git a/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/BookTest.java b/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/BookTest.java index ba5f8f5..c2c825b 100644 --- a/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/BookTest.java +++ b/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/BookTest.java @@ -1,10 +1,11 @@ package com.github.matschieu.jakartaee.validation; +import static org.assertj.core.api.Assertions.assertThat; + import java.util.ArrayList; import java.util.List; import java.util.Set; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import jakarta.validation.ConstraintViolation; @@ -24,17 +25,18 @@ public void testValidationMessage() { Set> violations = validator.validate(book); List messages = new ArrayList<>(); - Assertions.assertNotNull(violations); - Assertions.assertEquals(5, violations.size()); + assertThat(violations).isNotNull(); + assertThat(violations.size()).isEqualTo(5); violations.stream().forEach(v -> messages.add(v.getMessage())); violations.stream().forEach(System.out::println); - Assertions.assertTrue(messages.contains("Book title must not be null")); - Assertions.assertTrue(messages.contains("Book title must not be blank")); - Assertions.assertTrue(messages.contains("Book author must respect the regex [ a-zA-Z]*")); - Assertions.assertTrue(messages.contains("Book description must be between 1 and 50")); - Assertions.assertTrue(messages.contains("Book pages must be positive")); + assertThat(messages).contains("Book title must not be null"); + assertThat(messages).contains("Book title must not be null"); + assertThat(messages).contains("Book title must not be blank"); + assertThat(messages).contains("Book author must respect the regex [ a-zA-Z]*"); + assertThat(messages).contains("Book description must be between 1 and 50"); + assertThat(messages).contains("Book pages must be positive"); } } diff --git a/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/UserTest.java b/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/UserTest.java index e502371..a9fef23 100644 --- a/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/UserTest.java +++ b/md-jee-validation/src/test/java/com/github/matschieu/jakartaee/validation/UserTest.java @@ -1,12 +1,13 @@ package com.github.matschieu.jakartaee.validation; +import static org.assertj.core.api.Assertions.assertThat; + import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import jakarta.validation.ConstraintViolation; @@ -33,17 +34,17 @@ private String getAnnotationName(ConstraintViolation constraintViolation) { } private void checkConstraintViolations(String field, List> expectedConstraintViolations, Set> violations) { - Assertions.assertNotNull(violations); - Assertions.assertEquals(expectedConstraintViolations.size(), violations.size()); + assertThat(violations).isNotNull(); + assertThat(violations.size()).isEqualTo(expectedConstraintViolations.size()); List constraints = new ArrayList<>(); violations.stream().forEach(v -> { constraints.add(getAnnotationName(v)); - Assertions.assertEquals(field, v.getPropertyPath().toString()); + assertThat(v.getPropertyPath().toString()).isEqualTo(field); }); - expectedConstraintViolations.stream().forEach(c -> Assertions.assertTrue(constraints.contains(c.getCanonicalName()))); + expectedConstraintViolations.stream().forEach(c -> assertThat(constraints).contains(c.getCanonicalName())); violations.stream().forEach(System.out::println); } @@ -62,12 +63,10 @@ private User getValidUser() { @Test public void testValidUser() { - User user = getValidUser(); - - Set> violations = validator.validate(user); + Set> violations = validator.validate(getValidUser()); - Assertions.assertNotNull(violations); - Assertions.assertTrue(violations.isEmpty()); + assertThat(violations).isNotNull(); + assertThat(violations).isEmpty(); } @Test