How to test JPA in Arquillian - java

I am trying to use Arquillian to test my JPA repository classes. However I only get nullpointer exception telling me that it doesn't find the persistence.xml. How do you configure it in a standard Maven project?

Have you looked # the official documentation here. The project structure suggests that its built using Maven.
If you still run into issues do post the exception messages.
Good luck!

It looks like your ShrinkWrap deployment does not contain the persistence.xml in the right path. The persistence.xml file should be located in the META-INF directory of a JAR, or in WEB-INF/META-INF directory of a WAR. You could verify this in two ways:
Through the verbosity flag of the Archive.toString(...) method:
In your #Deployment method, you can print out the contents of the archive, using the toString method, like
#Deployment
public static Archive<?> createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class).addClasses(Foo.class);
System.out.println(war.toString(true));
return war;
}
Configuring Arquillian to write the generated deployment to disk:
You can add the engine configuration element to your arquillian.xml with the deploymentdeploymentExportPath property, like
<engine>
<property name="deploymentExportPath">target/deployment</property>
</engine>
This would instruct Arquillian to write the deployments it generates into a subdirectory under the target directory generated by Maven.

There is also bunch of examples in the showcase project on github, including JPA testing (also using Arquillian Persistence Extension).
Hopefully this will lead to you to the right path :)

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?

How to add classes from another to the Arquillian deployment archive?

I'm getting started with Arquillian and followed the tutorial. It covers how to inject EJBs into a test case. Since Arquillian is targeting Java Web and EE projects, I'm very suprised that the separation of entity classes and EJB interfaces wasn't covered in the tutorial since at least EE projects where everything is dumped in one project are rare.
Since there's no coverage in the tutorial and no understandable error message by any of the EE containers, I managed to extract a test case by trial and error which shows that classes used in
#Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(MyXBean.class, DefaultMyXBean.class, TermsOfUse.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
which are on the classpath, but not in the same Maven project the test in contained in, fail due to NoClassDefFoundError. Moving the class into the project without touching anything else makes the deployment work.
Is there any way to use Arquillian in a "real" Java EE in which entities and interfaces are not contained into the .war of the test, but as a sibling inside the parent .ear archive of the .war?
I could think of adding JARs from the Maven cache with hardcoded pathes, but that can't be it, can it?
How test in Java ee using Arquillian maven multiple project might be about the same issue.
An EAR deployment with libraries can be built like this:
#Deployment
public static Archive<?> createDeploymentPackage() throws IOException {
MavenDependencyResolver resolver = DependencyResolvers
.use(MavenDependencyResolver.class)
.loadMetadataFromPom("test1-pom.xml");
final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, "my-ejbs.jar").addClass(SomeEjb.class);
final WebArchive webApp = ShrinkWrap.create(WebArchive.class, "my-webap.war").addClass(MyServlet.class);
final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class)
.setApplicationXML("application.xml")
.addAsLibraries(resolver.artifact("log4j:log4j:1.2.17").resolveAsFiles())
.addAsModule(ejbJar)
.addAsModule(webApp);
return ear;
}

Weblogic Preprosessor in Maven

I have some Java code that is currently packaged in the BEA Workshop for WebLogic Platform.
My task is to migrate the structure of the project (without actually touching the code) to a maven structure to be packaged from command line (or from eclipse m2e).
Problem is, the code has some annotations like this:
#WebService(serviceName = "Cancelacion", targetNamespace =
"http://www.banamex.com.mx/OtorgamientoPension/cancelacion")
#WLHttpTransport(contextPath = "OtorgamientoPension", serviceUri =
"cancelacion", portName = "cancelacionSOAP")
#Policies({
#Policy(uri="policy:Wssp1.2-Wss1.0-X509-Basic256.xml", direction = Policy.Direction.inbound),
})
public class CancelacionPortImpl implements CancelacionPort {
...
}
That create some configuration inside the war (a mysterious meta-inf inside the web-inf and plenty of xml).
Please notice the #Policies which is from a WebLogic library. It creates some security-related config and that's (alongside the ws stuff) is what i want to generate.
Is there a way to process this from maven?
EDIT
So far I have tried with the weblogic-maven-plugin. It didn't work (also, due to internal policies, the not-so-straightforward way of installing this plugin is not an option).
I'm trying to find a vague reference a co-worker gave me about certain "jtools" compiler... but can't find anything that comes with that name and have some relation with WebLogic.
So the #Policies annotation is still a problem.
Right now I'm looking for a eclipse-plugin that does this, based on the premise that was the IDE who process that annotations.
For the wsdl issue, I find out that the namespace definition whas wrong. I corrected it and now it's working. I used the jaxb2-maven-plugin because I have no knowledge of jaxws-maven-plugin and I already had the config of the former.
Looks like you might need some Weblogic classes on your classpath. Short of uploading these to your own private Maven repository, you might consider checking out the Oracle Maven repository to find the Weblogic artifacts you need. Since these are likely container-provided jars (i.e. you don't need to package them in your war), you'll want to define them with a scope of "provided" in your dependencies, e.g. <scope>provided</scope>.

How to test not finding files on classpath? java (spring #Configuration)

I would like to test that a spring
#Configuration class
can handle missing files on the classpath. E.g. when using PropertySourcesPlaceholderConfigurer. But this is just a specific example, the question is really about how to test classes that interact with the classpath (e.g. read a file located in src/main/resources in a maven project).
So in essence I would like to create a spring context where I control the classpath in the test set up code.
The test needs to be a JUnit test.
Hope below may help you
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})
public class YourTestClass{
you have to create a spring context for your test and you can include the production one into it. you can replace classpath*: with a absolute location.
Regards, Rajib.
This work if it's a maven project:
move the classpath file that you want to test the absence from to a separate pom jar module, and include it wherever needed.
move the classpath test to a separate pom jar module named missing-classpath-file-test, but don't include the module with the file that you want to simulate as missing. I will be missing from the classpath only for that test.
When running missing-classpath-file-test, the file will not be on the classpath, and the error you need to reproduce is achieved.
Concerning the question on the comment bellow, with the class loaders that come with application servers and the one used on a junit test it's not possible to programmatically change the classpath.

Adding classpath to jetty running in maven integration-test

I'm trying to set up integration tests for a Maven project that produces a war file. (As seen here http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin/.) However I the war file requires a bunch of .properties files on the classpath, that I don't want to bundle in the war.
Is there a way (preferably through plugin configuration) to add a folder to the classpath used by jetty?
I Googled this and found http://markmail.org/message/awtqrgxxttra3uxx but this, as far as I can tell, does not actually work at all. The .properties files are not found.
This should be possible using the webAppConfig configuration element (sample below taken from this thread):
<webAppConfig>
<contextPath>/nportal</contextPath>
<!-- All I want to do here is add in the /etc/jetty/classes for runtime files. For some reason I have to also add back in the /target/classes directory -->
<extraClasspath>${basedir}/target/classes/;${basedir}/etc/jetty/classes/</extraClasspath>
</webAppConfig>
If you find that the above solution doesn't work for you, consider including the test classpath into your Jetty configuration.
<configuration>
<useTestClasspath>true</useTestClasspath>
...
</configuration>
This will then allow you to place all manner of resources/classes on the test classpath and have them visible to the Jetty server without them creeping into the production code.
You can place your additional configuration files under /src/test/resources and set a property <useTestScope>true</useTestScope> in the plugin configuration as specified here:
useTestScope
If true, the classes from testClassesDirectory and dependencies of scope "test" are placed first on the classpath. By default this is false.

Categories

Resources