Unit Testing for Java EE tech article on OTN

A new article, titled “Unit Testing for Java EE,” by Java Champion Adam Bien, is up on otn/java’s front page. Bien points out that too many developers believe that testing Java EE applications is too hard, inconvenient, or complex, something that has not been true since the advent of Java EE 5 more than five years ago.

Bien explains: “There is nothing special about unit testing Java EE 6 applications. You only have to add the JUnit library into your pom.xml file (see Listing 5) and put your classes into the src/test/java directory. All JUnit tests will be executed automatically during the standard Maven lifecycle: mvn clean install.”

He goes on to make use of “Mockito” an easy-to-use, open source mocking library. Bien writes:

“Mockito is able to create ‘smart proxies’ (a.k.a. mocks) from classes or interfaces. These proxies do not come with any behavior, but they are still perfectly usable. You can invoke methods, but will get the default values, or null, back. The behavior of the mocks can be recorded after their creation with when(mock.getAnswer()).then(42) syntax.

Mockito is perfectly suitable for ‘simulating’ any inconvenient classes, resources, or services. You can start with Mockito just by knowing a single class org.mockito.Mockito. The when-then ‘domain specific language’ is composed of static methods from the Mockito class. The org.mockito.Mockito class is well documented. In fact, the whole documentation set was generated from the JavaDoc markup in the org.mockito.Mockito class.”

Read the complete article here.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Insider News from the Java Team at Oracle!

duke
javeone logo
Links


Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
2
5
6
7
12
13
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today