Skip to content

Instantly share code, notes, and snippets.

@CedricGatay
Last active December 18, 2015 00:09
Show Gist options
  • Save CedricGatay/5694293 to your computer and use it in GitHub Desktop.
Save CedricGatay/5694293 to your computer and use it in GitHub Desktop.
Base test class for Wicket with CDI using Arquillian
/*
* Copyright 2013 Code-troopers.com
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package codetroopers.test.wicket;
import de.agilecoders.wicket.Bootstrap;
import de.agilecoders.wicket.settings.BootstrapSettings;
import org.apache.wicket.Application;
import org.apache.wicket.cdi.AutoConversation;
import org.apache.wicket.cdi.CdiConfiguration;
import org.apache.wicket.mock.MockApplication;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.resource.loader.ClassStringResourceLoader;
import org.apache.wicket.util.tester.WicketTester;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
/**
* @author cgatay
*/
@RunWith(Arquillian.class)
public abstract class AbstractCDIWicket_IT {
protected WicketTester wicketTester;
@Inject
BeanManager beanManager;
public static JavaArchive createBasicDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(AutoConversation.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Before
public void setUp() throws Exception {
wicketTester = new WicketTester(getWebApplication());
new CdiConfiguration(beanManager).configure(wicketTester.getApplication());
Bootstrap.install(wicketTester.getApplication(), new BootstrapSettings());
// trick to make Wicket tests use properties files named like the test as first resource for translation
wicketTester.getApplication().getResourceSettings().getStringResourceLoaders().add(0, new ClassStringResourceLoader(getClass()));
wicketTester.getApplication().getResourceSettings().getStringResourceLoaders().add(1, new ClassStringResourceLoader(getApplicationClass()));
}
protected Class<? extends Application> getApplicationClass() {
return MockApplication.class;
}
@After
public void tearDown(){
wicketTester.getApplication().internalDestroy();
}
protected WebApplication getWebApplication() {
return new MockApplication();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment