Skip to content

Commit

Permalink
Use AssertJ instead of assertions provided by JUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
matschieu committed Jan 19, 2025
1 parent ae8104c commit 4a6809c
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 220 deletions.
5 changes: 5 additions & 0 deletions md-jee-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Shop<Book>>() {}).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<Shop<Book>>() {}).get()).isInstanceOf(BookShop.class);
}

@Inject
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,36 +25,33 @@ 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
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();
}

}
Loading

0 comments on commit 4a6809c

Please sign in to comment.