Skip to content

Commit

Permalink
Extend DAOTestExtension with BeforeAll/AfterAll callbacks (dropwizard…
Browse files Browse the repository at this point in the history
…#4680)

# Problem
The `DAOTestExtension` currently does not implement the Junit5 `BeforeAll` and `AfterAll` callbacks, therefore its not possible to register it as a proper extension during testing.

# Solution
Implement the `BeforeAllCallback` and `AfterAllCallback` interfaces.

# Result
`DAOTestExtension` can be registered as a proper extension during testing using the `@RegisterExtension` annotation.
  • Loading branch information
eikenilsknopp authored Feb 7, 2022
1 parent 786cad5 commit 3493ae9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.dropwizard.testing.junit5;

import io.dropwizard.testing.common.DAOTest;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.util.concurrent.Callable;

Expand Down Expand Up @@ -34,7 +38,7 @@ public void createPerson() {
* </p>
*/
//@formatter:on
public class DAOTestExtension implements DropwizardExtension {
public class DAOTestExtension implements DropwizardExtension, BeforeAllCallback, AfterAllCallback {
private final DAOTest daoTest;

public static class Builder extends DAOTest.Builder<Builder> {
Expand Down Expand Up @@ -70,6 +74,20 @@ public void after() {
daoTest.after();
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
try {
before();
} catch (Throwable e) {
throw new HibernateException(e);
}
}

@Override
public void afterAll(ExtensionContext extensionContext) {
after();
}

/**
* Returns the current active session factory for injecting to DAOs.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.dropwizard.testing.junit5;

import io.dropwizard.testing.app.TestEntity;
import org.hibernate.SessionFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;


class DAOTestExtensionRegisterExtensionTest {

@RegisterExtension
static final DAOTestExtension daoTestExtension =
DAOTestExtension.newBuilder()
.addEntityClass(TestEntity.class)
.build();

@Test
void shouldProvideSession() {
final SessionFactory sessionFactory = daoTestExtension.getSessionFactory();
assertThat(sessionFactory).isNotNull();
}
}

0 comments on commit 3493ae9

Please sign in to comment.