Skip to content

Instantly share code, notes, and snippets.

@luke10x
Last active June 20, 2023 00:25
Show Gist options
  • Save luke10x/be93d82bfa544962f1541ee7de1e91b8 to your computer and use it in GitHub Desktop.
Save luke10x/be93d82bfa544962f1541ee7de1e91b8 to your computer and use it in GitHub Desktop.
Alternatives to Arquillian

About dependency choice:

Wildfly + Resteasy is the best stack for production and it is from Red Hat

  • krazo-resteasy - is an extension for Wildfly
  • krazo-cxf - is an extension for TomEE
  • krazo-jersey - is an extension for Glassfish and Payara

Even though RestEasy is used for prod, Jersey is easier to setup in test environment, To use Jersey in tests full Krazo extension is not needed, instead lightweight In-Memory implementation from Jersey Test Framework is used it is not a real container. It call APIS directly without any communication over network.

// In memory JAX-RS implementation for tests
testImplementation 'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:3.1.1'
testImplementation 'org.glassfish.jersey.test-framework:jersey-test-framework-core:3.1.1'
testImplementation('org.glassfish.jersey.inject:jersey-hk2:3.1.1') {
exclude group: 'org.glassfish.hk2.external', module: 'javax.inject'
}
implementation 'org.glassfish.jersey.ext:jersey-bean-validation:3.1.1'
implementation 'org.jboss.resteasy:resteasy-jaxrs:3.15.6.Final'
implementation 'org.jboss.resteasy:jaxrs-api:3.0.12.Final'
implementation 'org.jboss.resteasy:resteasy-jaxb-provider:6.2.3.Final'
implementation 'org.jboss.resteasy:resteasy-jackson2-provider:6.2.3.Final'
testImplementation 'org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-gradle-embedded-archive:3.1.4'
testImplementation 'org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-api-maven-embedded:3.1.4'
//
implementation 'org.glassfish.jersey.media:jersey-media-multipart:3.1.1'
// testImplementation 'org.jboss.resteasy:resteasy-client:6.2.4.Final'
testImplementation 'org.jboss.resteasy:resteasy-jackson2-provider:6.2.4.Final'
implementation 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
// For CDI tests
testImplementation 'de.hilling.junit.cdi:cdi-test-core:4.3.0'
testImplementation 'de.hilling.junit.cdi:cdi-test-jee:4.3.0'
testImplementation 'de.hilling.junit.cdi:cdi-test-microprofile:4.3.0'
// For Weld junit5:
implementation 'org.jboss.weld:weld-junit5:4.0.0.Final'
package dev.luke10x.easylogin.registration;
//@ExtendWith(WeldJunit5Extension.class)
import de.hilling.junit.cdi.CdiTestJunitExtension;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(CdiTestJunitExtension.class)
@ExtendWith(MockitoExtension.class)
public class RegistrationControllerCdiTest {
@Inject RegistrationController registrationController;
@Test
public void myFirstCdiTest(@Mock RegistrationService registrationService) {
registrationController.register();
}
}
package dev.luke10x.easylogin.registration;
import dev.luke10x.easylogin.common.FlashContainer;
import jakarta.inject.Inject;
import jakarta.mvc.binding.BindingResult;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.Provider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static jakarta.ws.rs.core.Response.Status.*;
import static org.jboss.modules.management.ObjectProperties.property;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
public class RegistrationControllerJerseyTest extends JerseyTest {
@Mock
private RegistrationFormMapper registrationFormMapper;
@Mock
private RegistrationService registrationService;
@Mock
RegistrationModel registrationModel;
@Inject
BindingResult bindingResult;
@Mock
FlashContainer flashContainer;
@Provider
public static class BindingResultContextResolver implements ContextResolver<BindingResult> {
private final BindingResult bindingResult;
@Inject
public BindingResultContextResolver(BindingResult bindingResult) {
this.bindingResult = bindingResult;
}
@Override
public BindingResult getContext(Class<?> type) {
return bindingResult;
}
}
@Override
protected Application configure() {
MockitoAnnotations.openMocks(this);
// Configure the JAX-RS application with your controller and dependencies
return new ResourceConfig(RegistrationController.class)
.register(new AbstractBinder() {
@Override
protected void configure() {
bind(registrationFormMapper).to(RegistrationFormMapper.class);
bind(registrationService).to(RegistrationService.class);
bind(registrationModel).to(RegistrationModel.class);
// bind(bindingResult).to(BindingResult.class);
bind(flashContainer).to(FlashContainer.class);
}
})
.packages("org.glassfish.jersey.examples.multipart")
.register(MultiPartFeature.class);
}
// public ViewApplicationConfig() {
// packages("com.baeldung.jersey.server");
// property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, Boolean.TRUE.toString());
// }
// @Override
// protected void configureClient(ClientConfig config) {
// config.register(MultiPartFeature.class);
// }
@Test
public void validationTest() {
var response = target("register").request().get(Response.class);
assertEquals(ACCEPTED, response.getStatus());
assertEquals("REDIRECTION", response.getStatusInfo().getFamily().name());
assertEquals("See Other", response.getStatusInfo().getReasonPhrase());
assertEquals("onboarding", response.getHeaders().getFirst("Location").toString());
}
@Test
public void validationTriesToBindErrors() {
// when(bindingResult.isFailed()).thenReturn(true);
final Form form = new Form();
form.param("handle", "this_is_me");
Response response = target("register").request().post(Entity.form(form));
assertEquals(BAD_REQUEST, response.getStatus());
assertEquals("REDIRECTION", response.getStatusInfo().getFamily().name());
assertEquals("See Other", response.getStatusInfo().getReasonPhrase());
assertEquals("onboarding", response.getHeaders().getFirst("Location").toString());
}
@Test
public void validationDifferently() {
}
}
package dev.luke10x.easylogin.registration;
import dev.luke10x.easylogin.adapter.RegistrationServiceImpl;
import dev.luke10x.easylogin.common.FlashContainer;
import dev.luke10x.easylogin.topt.TotpGenerator;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.inject.Inject;
import jakarta.mvc.binding.BindingResult;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;
import org.jboss.weld.junit.MockBean;
import org.jboss.weld.junit5.WeldInitiator;
import org.jboss.weld.junit5.WeldJunit5Extension;
import org.jboss.weld.junit5.WeldSetup;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
@ExtendWith(WeldJunit5Extension.class)
@WeldSetup
public class RegistrationControllerWeldTest {
// @Inject RegistrationController registrationController;
// @ApplicationScoped
// @Produces
// public RegistrationService registrationService() {
// return Mockito.mock(RegistrationService.class);
// }
static Bean<?> createBarBean() {
return MockBean.builder()
.types(RegistrationService.class)
.scope(ApplicationScoped.class)
.creating(
Mockito.mock(RegistrationService.class)
)
.build();
}
static Bean<?> createBindingResultBean() {
return MockBean.builder()
.types(BindingResult.class)
.scope(RequestScoped.class)
.creating(
Mockito.mock(RegistrationService.class)
)
.build();
}
@WeldSetup
public WeldInitiator weld = WeldInitiator
.from(
RegistrationController.class,
FlashContainer.class,
RegistrationModel.class,
RegistrationFormMapper.class,
TotpGenerator.class
)
.addBeans(createBarBean(), createBindingResultBean())
.activate(RequestScoped.class)
.build();
@Test
void registerTest() {
weld.select(RegistrationController.class).get().register();
}
@Test
void registrationWithFaultyForm() {
var form = new RegistrationForm();
form.setHandle(""); // hould be validated
try {
Response response = weld.select(RegistrationController.class).get().handleSubmit(form);
assertEquals(333, response.getStatus());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment