How do I configure arquillian suite extension - java

How do I configure Arquillian Suite extesion?
https://github.com/it-crowd/arquillian-suite-extension
I would like to use it for single deployment tests, in order not to have to deploy for every single class that have #Test methods in my project.
By the way, I'm using TESTNG with arquillian..

I pushed extension bit further, it can be found on maven central and there is part of help written + tests to look how it should be done.
I also created "generic" deployer builder that should work with javaee6.
https://github.com/ingwarsw/arquillian-suite-extension

Related

Using SpringExtension with Tomcat and Hibernate web app

We are trying to use spring-test's SpringExtension to write integration tests for our Spring and Hibernate-based Tomcat web application. Our sessionFactory bean configuration has the property configured mappingJarLocations with a sample value as /WEB-INF/lib/company-common*.jar which contains hibernate mapping files. In both actual deployment and Eclipse dev deployment, this works fine as the docBasePath (in Servlet environment) is appended to this pattern and the files are getting resolved. But this is not the case while running JUnit test cases either in a local or a CI environment.
We tried our best to use the provided support by having few overridden implementations of WebTestContextBootstraper, GenricXmlWebContextLoader, XmlWebApplicationContext, and WebDelegatingSmartContextLoader but had to finally give up as we cannot override the final method org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(MergedContextConfiguration) to provide the custom implementation of XmlWebApplicationContext. Our current approach is to manually create the application context and use it in the tests.
Here is the project structure:
Project_WebApp
|--src/**
|--WebContent/**
|--pom.xml
When the app is packaged as Project_WebApp.war, the dependencies are inside WEB-INF/lib from the root of extracted war. When deployed as a webapp in Tomcat using Eclipse, the dependencies are copied to <Eclipse_Workspace_Dir>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/Project_WebApp/WEB-INF/lib. In both cases, the dependencies are available at <Resource_Base_Path>/WEB-INF/lib and Resource_Base_Path has no relation to Project_WebApp base directory.
Questions:
Did any one use SpringExtension in a scenario similar to above? If so can you suggest any alternative approaches?
Instead of /WEB-INF/lib/company-common*.jar, we tried a classpath-based pattern but didn't work as the obtained class path resources don't match the pattern. Is there anything else to try here?

Spring boot integration test with environment variables

I'm working on a multi-module maven project using Java 8, Spring Boot 2.4.0. I want to test one of a module that is calling a 3rd party service. I'm using wiremock to mock that 3rd party service call and have created a spring boot integration test class. My class is in the same package where my XYZService class is. My test is in src/test/... and looks like this.
#RunWith(SpringRunner.class)
#SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)
public class XYZServiceIntegrationTest {
#Rule
public WireMockRule externalService = new WireMockRule();
#Test
public void test1() {...}
#Test
public void test2() {...}
}
When I run the maven build in my eclipse (clean + install). My build is not detecting the tests present at the maven-module where the Integration test is located. The other unit tests in the same module are also not being detected (Note: before adding the integration test class, the unit tests were working). Maven says Tests ran: 0. The integration test is working fine when I Right click on the file and run as JUnit test(Junit 4). Also, I have some environment variables that need to be set for running the SpringApplicationClassWithMainMethod.class that I'm setting within the configurations of that Integration test class in order to successfully load the Application context(I tried to load the environment variables through code and nothing from other stack-overflow posts worked). One more thing to inform my maven only uses Maven surefire plugin for running tests. I dont know if we need to have Maven fail safe plugin for my purpose(Is my test considered a Integration Test when I added the 2 annotations on top of the test class?). Can someone please help me with any suggestions on how to build the parent project.
Adding the maven-failsafe-plugin into my child pom worked. In the parent pom I added the execution goals for maven-failsafe-plugin in the and in the child pom I inherited that plugin.

Jenkins Gradle test fails on Commons Configuration

I have a project that uses Apache Commons Configuration. The project is built using gradle. I have some unit test cases written on this project and gradle test works fine when run locally.
However when the unit tests are run in Jenkins it fails.
Please see the screenshot of the error. The error seems to be to do something with Commons Configuration that I am using. Please help.
org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
org.apache.commons.configuration.ConfigurationException: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
at org.apache.commons.configuration.DefaultConfigurationBuilder.createConfigurationAt(DefaultConfigurationBuilder.java:752) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.initCombinedConfiguration(DefaultConfigurationBuilder.java:628) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:560) ~[commons-configuration-1.6.jar:1.6]
The ConfigurationProvider try to load a configuration xml file which is default the config.xml.
In my case the project is build using Jenkins. Jenkins provides a config.xml in Jenkins home dir. This is loaded first instead of my desired one. Maybe that applies for you too?
Example Jenkins config.xml
<?xml version=’1.1' encoding=’UTF-8'?>
<hudson>
<disabledAdministrativeMonitors>
...
</disabledAdministrativeMonitors>
...

Project organization using Maven and ParameterSupplier

I am wondering what would be some suggestions for project / module organization given the following situation:
I have a project DomainObjects in which I have a class MyObject
In /src/test/java of DomainObjects I have the tests for MyObject
I have a project Client that depends on DomainObjects
I would like to add a ParameterSupplier called MyObjectTestSupplier class to provide test instances of MyObject for use by tests in Client.
It seems to make the most sense to provide MyObjectTestSupplier in the DomainObjects project. Here is my dilema...
if I put the supplier in src/test/java of DomainObjects it will not be available to Client.
I don't want to put it in src/main/java of DomainObjects because that means that JUnit would have to be included as a compile dependancy of DomainObjects and thereby be included in my production code.
if I put the supplier in some project DomainObjectsTest I have three options
put just the supplier is the test project but this means that tests in DomainObjects could not use this supplier.
put all the tests and suppliers for DomainObjects in DomainObjectsTest but that means that DomainObjects will be successfully compiled by maven even if tests fail
copy the supplier in both src/test/java of DomainObjects and src/main/java of DomainObjectsTest.
I thought about trying to make DomainObjectsTest a module of DomainObjects but that only works if the packaging for DomainObjects is pom which does not work here.
Thoughts? Suggestions?
EDIT: As an explanation, MyObject is a simple bean (just getters and setters) and I use the ParameterSuppier pattern for providing populated instances of beans. The supplier provides utility methods to easily create populated instances of the bean for use in testing. I do this so that I don't repeat this population code (or the mocking equivilent) throughout my project(s).
As the official Maven mini guide on this particular topic says you should publish a test artifact of the DomainObjects project into your local Maven repository (or anywhere you'd like or able to) and use the DomainObjects-X.Y-tests artifact as a test-scoped dependency in your Client project.
Publishing a test artifact is done by using the jar:test-jar goal of the Maven JAR plugin.
If you include this artifact as a test-scoped dependency in your Client project then any other project that depends on the Client project won't inherit your DomainObjects project's test artifact, because test-scoped dependencies are not transitive by default as stated by the official guide on Maven's dependency mechanism.
That is a bad design example what you're describing. Your unit test can't depend on external dependencies to be "unit".
What you need to do is to mock all dependencies and test only Client code. Use Mockito or another library of your choice to create mock instances of MyObject in client project according to what you expect this class to do. Test MyObject behavior in its own project - DomainObjects.
In mockito creating a mock is just :
import static org.mockito.Mockito.*;
...
MyObject myMock = mock(MyObject.class);
when(myMock.doWhatYouNeed(params)).thenReturn(whatYouExpect);
Edit:
Another ideas
Publish DomainObjects' tests as artifact of type test-jar as descibed here and use it as test-scoped dependency in Client. But this is quite ugly...
Nice design is :
DomainObjectAPI project with MyObject,
DomainObjectTestSupplier using DomainObjectAPI providing suppliers,
DomainObject using DomainObjectAPI for compile and DomainObjectTestSupplier for testing
Client using DomainObjectAPI, DomainObject for compile and DomainObjectTestSupplier for testing.
It's just an overkill.

Maven - use different java classes during 'test' and 'war' phase

I'm using maven war plugin to build war package.
Before package is build test are executed. To preinitialize my database with sample data I use spring bean. I would like to have different data in my db for tests and different when application starts.
I was thinking that maybe it is possible to use two different spring initializer classes in 'test' and 'war' phases but I don't know how to achieve this.
You have to put the different classes you need into src/main/java or src/test/java or may be supplemental application.xml into src/main/resources or src/test/resources. The test initializer can be done by a Test class which initializes first before all tests are running (take a look at testng which has this kind of feature).
Your tests should not be using the production Spring context (xml) files.
Instead, if you need to access an ApplicationContext in your tests (or if you are using a base testcase class like AbstractTransactionalJUnit4SpringContextTests), set up a test-context.xml context which points to the test database configuration and the test data scripts.

Categories

Resources