Adam Bien's Weblog

Monday Sep 15, 2014

Unit / Integration Testing the Bean Validation (JSR-303)

First you will need to load both: the Bean Validation and EL SPIs. Hibernate Validator is the Reference Implementation (RI) for Bean Validation, and GlassFish implements the EL JSR:


	   <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.2.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
            <scope>test</scope>
        </dependency>

Hibernate Validator requires Expression Language dependency. Without the javax.el dependency you will get the following error:


Caused by: javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath
	at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.(ResourceBundleMessageInterpolator.java:172)
	at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.(ResourceBundleMessageInterpolator.java:118)

Now you are ready to launch your Integration Test:


import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

public class ValidationTest {

    private Validator validator;

    @Before
    public void init() {

        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        this.validator = vf.getValidator();

    }

    @Test
    public void prereqsMet() {
        Workshop validWorkshop = new Workshop(2, 2, true, 3);
        Set<ConstraintViolation<Workshop>> violations = this.validator.validate(validWorkshop);
        assertTrue(violations.isEmpty());
    }  
}

Strictly speaking it is not a unit test, rather an Integration Test. In Unit Test you would like to test the validator logic only, without any dependencies to the SPI.

See you at Java EE Workshops at MUC Airport, especially at the Java EE 7 / Java 8 Testing day!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Saturday Sep 13, 2014

Java EE Maven Archetype (BCE / ECB) v0.0.4 - EntityManager Mocking and System Test

The 0.0.4 version of Java EE Maven Archetype comes with JPA EntityManager mocking example and dedicated JAX-RS system tests.

Execute the javaee-bce-archetype archetype from the command line and choose version v0.0.4 to create a sample Java EE project with Boundary / Control / Entity (BCE) structure, unit-, integration-, stress-, and system tested:


mvn archetype:generate -Dfilter=com.airhacks:javaee-bce-archetype

The sources of the sample project are also available directly: https://github.com/AdamBien/javaee-bce-pom.

Thanks Dirk Franssen for the pull request!

See you at Java EE / HTML 5 workshops at Airport Munich and each first Monday of the month at 6 P.M. (CET) airhacks.io


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Friday Sep 12, 2014

WildFly, Bower, Karma, RequireJS or NetBeans 8.0.1

NetBeans IDE 8.0.1 is available for download and contains:

  1. Modularity and enterprise features for JavaScript via RequireJS
  2. Support for debugging JavaScript files with Karma
  3. Node.JS and Bower modules can be installed directly within the IDE
  4. Grunt tasks available in the popup menu for web projects
  5. Built-in support for: GlassFish 4.1, Tomcat 8.0.9, WildFly, and WebLogic 12.1.3
  6. Latest PrimeFaces framework bundled in the IDE
  7. GIT and Java tools improvement

All the features above are available without any additional plugin :-)

See the entire feature list.


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Thursday Sep 11, 2014

HTML5, WebSockets and Java EE 7 -- Screencast

A simplest possible example for HTML - Java EE 7 communication using WebSockets.

See also other screencasts at: http://tv.adam-bien.com or subscribe to http://www.youtube.com/user/bienadam.

See you at Java EE Workshops at Munich's Airport, particularly at the Java EE User Interfaces or / and Java EE 7 and HTML 5 workshops!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Wednesday Sep 10, 2014

GlassFish 4.1 Is Ready For Download

GlassFish 4.1 is available for download.

Checkout the release notes for fixes / features.

See you at Java EE Workshops at MUC Airport or on demand and in a location very near you: airhacks.io!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Saturday Sep 06, 2014

Recordings Of The 6th Airhacks Q&A

Thanks for watching live (79 attendees), all the interactions, particularly from the IRC #airacks channel and twitter.

Any questions left? Then join the conversation at each first Monday of the month at 6 P.M. live. No registration or any other commitment required.

Also checkout: "Timisoara JUG Session: Hacking Opinionated JavaFX / Java 8 Apps" at 16th October, 5.30 P.M CET: http://www.ustream.tv/channel/adambien.

See also other screencasts at: http://tv.adam-bien.com or subscribe to http://www.youtube.com/user/bienadam.

See you at Java EE Workshops at MUC Airport or on demand and in a location very near you: airhacks.io!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Friday Sep 05, 2014

Java 8 + Java EE 7: POJO to JsonObject Conversion

With Java 8 a java.util.List of domain objects can be easily converted into a javax.json.JsonArray using a Collector created by the Collectors utility. javax.json.JsonObject and javax.json.JsonArray have to be created with the corresponding builder, what makes a Collector implementation necessary:


 public JsonArray allAsJson() {
		
        Collector<JsonObject, ?, JsonArrayBuilder> jsonCollector
                = Collector.of(Json::createArrayBuilder, JsonArrayBuilder::add,
                        (left, right) -> {
                            left.add(right);
                            return left;
                        });
        return all().stream().map(Registrations::convert).
                collect(jsonCollector).build();

    }
	
    static JsonObject convert(Registration registration) {
        return Json.createObjectBuilder().
                add("price", registration.getTotalPrice()).
                add(CONFIRMATION_ID, registration.getId()).build();
    }


The snippet above is a part of the https://github.com/AdamBien/javaee-bce-archetype Java EE 7 / Java 8 sample project and can be easily installed using: mvn archetype:generate -Dfilter=com.airhacks:javaee-bce-archetype.

See you at Java EE Workshops at Munich Airport or on demand and in a location very near you: airhacks.io!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Wednesday Sep 03, 2014

Building Nanoservices with Java 8 and JavaEE 7

Microservices were yesterday. Now talk about nanoservices:

Stay tuned: Picoservices are around the corner! :-)

Thanks to foocafe.org for the invitation and perfect organization!

See you at Java EE Workshops at Munich Airport or on demand and in a location very near you: airhacks.io!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Tuesday Sep 02, 2014

The Perfect JavaEE Microservice

Microservices are a self-contained and easily understandable realization of domain logic, highly independent of each other.

The definition above could be shortened as: "Maximal Cohesion, Minimal Coupling" (MC/MC), which in turn defines a Java EE business component. A Java EE component is an ordinary Java package organized with the MC/MC idea in mind, ideally with predefined internal structure like e.g. BCE / ECB.

According to the above definition, a perfect JavaEE microservice is single ECB component within a WAR deployed on a single server/domain. In such a case you could release and redeploy individual components (aka microservices) independently. Direct method invocations between WARs are not possible, so the WARs are forced to use e.g. JAX-RS to communicate with each other.

Monolithic deployment of multiple components within a single WAR still remains the simplest possible solution for a mainstream project without any additional requirements. Unfortunately, simplest possible solutions are usually not buzzword-compatible :-).

[See also an in-depth discussion in the "Real World Java EE Patterns--Rethinking Best Practices" book (Second Iteration, "Green Book"), page 419 in, chapter "Entity Control Boundary (ECB)—The Lean Way"]

See you at Java EE Workshops at MUC Airport, particularly at the Java EE User Architectures workshop!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

Sunday Aug 31, 2014

Opinionated JavaFX 8--virtualJUG Session

Opinionated Java FX applications, behind the scenes of afterburner.fx and walk through the floyd at virtualjug.

Thanks http://virtualjug.com for the invitation!

See you at Java EE Workshops at MUC Airport, particularly at the Java EE User Interfaces workshop!


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns

realworldpatterns.com
...the last 150 posts
...the last 10 comments
Links
License