Arquillian with NetBeans, GlassFish embedded, JPA and a MySQL Datasource
Join the DZone community and get the full member experience.
Join For FreeThis is an, let's call it accidental post. I was looking into
transactional CDI observers and playing around with GlassFish embedded
to run some integration tests against it. But surprisingly this did not
work too well and I am still figuring out, where exactly the problems
are while using the plain embedded GlassFish for that. In the meantime I
switched to Arquillian. After I looked at the Arquillian 1.0.0.Alpha4 a bit more detailed last year,
it was time to see, what the guys prepared for 1.0.0 final. Some stuff
changed a bit and I was surprised to only find some basic examples on
the net. That was the main reason I started writing this post. To have a
more complete example of all the three technologies working together.
Getting Started
First of all, get yourself a fresh copy of latest NetBeans 7.1.
It's out since a few weeks and it really looks good. If you like, you
can download a copy of the Java EE version, which comes with latest
GlassFish OSS Edition 3.1.1 pre bundled. Install it and fire it up. Also
grep a copy of MySQL Community Edition
and install it.
Back in NetBeans, setup a new Maven Webapplication via the new Project
Wizard. Let's call it "simpleweb" and we tell it to run on GF 3.1.1 (if
you haven't done so, you should create a Services>Server>GlassFish
instance before or do it during the project setup).
Implement your Application
Even if this isn't test-driven, I like to use this approach because I
still believe, that this is the most common case you find out there in
your projects. You have a lot of stuff ready to run and you are looking
for some kind of automated integration tests. So, let's assume you have a
very basic entity, we call it
"com.mycompany.simpleweb.entities.AuditLog". You can create it with the
NetBeans new Entity wizard. The third step during the Entity creation is
the provider and database setup. Define a new Datasource and name it
"jdbc/auditlog". As a connection specification use MySQL J driver and I
assume you have a database up and running (let's assume this is called
auditlog). Test the connection and finish the wizard.
If you are using the wizard, you get some free gifts. Beside the fact, that you now have your AuditLog entity in the source tree, you also find a META-INF/persistence.xml file in your src/main/resources and a glassfish-resources.xml in src/main/setup. This is needed later on, keep it in mind. Add some additional properties to your entity. For now I add "String account". And don't forget to define a Version field "Timestamp timestamp". And it's also nice to have a little named query to get a list of all AuditLogs
Add some code to it to insert an AuditLog:
Adding Basic Test Dependencies
Next we are going to add some very basic test dependencies. Open your projects pom.xml file and add the following sections for Arquillian to your project:
Configuring Arquillian
After we have all the needed dependencies in place, we need to further configure Arquillian. This is done via the arquillian.xml which has to be placed in the src/test/resources folder (you might need to create it outside NetBeans before) and should look like this:
Add a Testcase
Lets add a test. This is easy with NetBeans: Right click on your EJB and select "Tools>Create JUnit Tests". Select JUnit 4.x and accept the name proposal "com.mycompany.simpleweb.service.AuditRepositoryServiceTest". Now your project has a new "Test Packages" folder. As you can see, the test is in error. NetBeans assumes, that you want to do a test based on embedded EJBContainer. Nice guess, but we would like to add some Arquillian here. Remove the EJBContainer import and strip the class down to this:
Examining what's happening
The output window shows the Std.out of a starting GlassFish. If you examine the output further you see, that the JDBC connection pool and the JDBC resource are created:
INFO: command add-resources result: PlainTextActionReporterSUCCESSDescription: add-resources AdminCommandnull JDBC connection pool mysql_auditlog_rootPool created successfully. JDBC resource jdbc/auditlog created successfully.and the "test" application was deployed:
INFO: WEB0671: Loading application [test] at [/test] 17.01.2012 10:12:39 org.glassfish.deployment.admin.DeployCommand execute INFO: test was successfully deployed in 6.461 milliseconds.Scanning through the output does point you to some EclipseLink stuff, but the additional sql logging doesn't seem to be in effect. This is because EclipseLink needs to know to which logger to point the output to. Normally the log output is redirected to the server logger which is auto-discovered. We didn't do any logging configuration until now and simply rely on what is default for Java Logging. So, let's add a little logging configuration. Put an empty logging.properties file to src/test/resources and add some simple lines to it:
handlers=java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.ConsoleHandler.level=FINESTadd the maven sure-fire plugin to the build section of your pom.xml:
FEIN: INSERT INTO AUDITLOG (ID, ACCOUNT, TIMESTAMP) VALUES (?, ?, ?) bind => [1, Markus, 2012-01-17 11:02:54.0]In fact, if you use Strg+F6 you still only see INFO level messages. To fix this, you need to change your NetBeans run action settings. Right click your project and select "Properties". Select "Actions" and "Test file". Add the following as a new line within "Set Properties" area:
java.util.logging.config.file=src/test/resources/logging.properties
Now you also see the desired log-level output with a single test-run. That was it. You can download the complete sample maven project (as ZIP-File) with the mentioned classes and play around a bit. Have fun!
From http://blog.eisele.net/2012/01/arquillian-with-netbeans-glassfish.html
Opinions expressed by DZone contributors are their own.
Trending
-
An Overview of Kubernetes Security Projects at KubeCon Europe 2023
-
Is Podman a Drop-in Replacement for Docker?
-
4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
-
Transactional Outbox Patterns Step by Step With Spring and Kotlin
Comments