Testing with JUnit in ADF

When you are coding, certainly you will need to test the code to ensure that new features, bug fixes, or refactorings do not destabilize your application. JDeveloper provides the ability to generate JUnit test case, test fixture and test suite to test your ADF application.

In this post, I will create a JUnit Test Case to test a method of my Application Module.
Download the sample application:

Installing JUnit

In the main menu, choose Help and then Check For Updates.
Select Official Oracle Extensions and Updates and click Next.
Select JUnit Integration and click Next.


In the License Agreements page, click the I Agree button, and then click Next.
Wait the download finish and click Finish.
In the Confirm Exit dialog, click the Yes button to restart the JDeveloper.

Creating JUnit Test Case

Generate the Employees Implementation with Accessors.


Generate the Application Module Implementation and add the following code.

 * Helper method to return an Employee by Id
private EmployeesImpl retrieveEmployeeById(Integer employeeId) {
  EntityDefImpl employeesDef = EmployeesImpl.getDefinitionObject();
  Key orderKey = EmployeesImpl.createPrimaryKey(employeeId);
  return (EmployeesImpl) employeesDef.findByPrimaryKey(getDBTransaction(), orderKey);

 * Find an Employee and its Manager
public String findEmployeeAndManager(Integer employeeId) {
  EmployeesImpl employee = retrieveEmployeeById(employeeId);
  if (employee != null) {
    EmployeesImpl manager = retrieveEmployeeById(employee.getManagerId());
    if (manager != null) {
          "Employee: " + employee.getLastName() + ", " + employee.getFirstName() + 
          ", Manager: " + employee.getLastName() + ", " + employee.getFirstName();
    } else {
      return "Unassigned";
  } else {
    return null;


To keep the JUnit Unit Tests separate from the rest of the application code, create a separate project to hold the unit tests.
In the main menu, choose File > New > Project.
Choose the Custom Project, name it as ModelTest and click Finish.
Double-click the ModelTest project and add the Model Project as a dependency.


Create a Test Fixture, the class that will be used by all of the tests to obtain an instance of the application module.
In the Applications window, right-click the ModelTest project and choose New > From Gallery.
Choose General > Unit Tests > Test Fixture, and click OK.
Change the name to AppModuleFixture and the package to, and click OK.


Replace the content of AppModuleFixture class with the following code.



import oracle.jbo.ApplicationModule;
import oracle.jbo.client.Configuration;

public class AppModuleFixture {
  private ApplicationModule applicationModule;
  private AppModuleImpl appModuleImpl;

  public AppModuleFixture() {}

  public void setUp() {
    String amDef = "";
    String config = "AppModuleLocal";
    applicationModule = Configuration.createRootApplicationModule(amDef, config);
    appModuleImpl = (AppModuleImpl) applicationModule;

  public void tearDown() {
    Configuration.releaseRootApplicationModule(applicationModule, true);

  public AppModuleImpl getAppModuleImpl() {
    return appModuleImpl;


Create the Unit Test class for the AppModule.
In the Applications window, right-click the ModelTest project and choose New > From Gallery.
Choose General > Unit Tests > Test Case, and click OK.
In the Step 1, click the Browse button and choose the AppModuleImpl class.


Check the findEmployeeAndManager() method to test it, and click Next.


In the Step 2, add “.test” at the end of Package, check the setUp() and tearDown() methods, and click Finish.
The setUp() method initializes the resources required by the test case, and the tearDown() method clean them up.


In the Step 3, click the Browse button and choose the AppModuleFixture class.


Click Finish.
Replace the content of AppModuleImplTest class with the following code.



import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class AppModuleImplTest {
  AppModuleFixture fixture1 = new AppModuleFixture();

  public AppModuleImplTest() {}

  public void setUp() throws Exception {

  public void tearDown() throws Exception {

   * @see
  public void testFindEmployeeAndManager() {
    AppModuleImpl appModuleImpl = fixture1.getAppModuleImpl();

    int employeeId = 101;
    String ret = appModuleImpl.findEmployeeAndManager(employeeId);
    assertTrue("Employee without manager", ret.equals("Unassigned"));

    employeeId = 102;
    ret = appModuleImpl.findEmployeeAndManager(employeeId);
    assertTrue("Employee with manager", ret.contains("Employee"));

    employeeId = 1000;
    ret = appModuleImpl.findEmployeeAndManager(employeeId);
    assertTrue("Nonexistent employee", ret == null);


Running JUnit test

Run the test!
In the Applications window, right-click the AppModuleImplTest file and choose Run As Unit Test.
This starts JUnit and executes all test methods in this class.


The test is failing, because the Employee with ID 101 has a manager.
Change the ID of Employee to 100 and re-run the test.


1 reply on “Testing with JUnit in ADF”

Comments are closed.