Enterprise Java

Unit testing of ADF Application using JUnit

JUnit is a unit testing package for Java language and can be used to test Oracle ADF application as ADF is built on top of the J2EE framework. Unit testing is basically is a process to verify the smallest testable module against some defined test criteria. Here I am going to illustrate that how can we set up and use JUnit in JDeveloper 12.2.1.3 to test ADF application.

JDeveloper 12.2.1.3 comes with JUnit extension so no need to install it separately. Let’s start by creating a Fusion Web Application in JDeveloper IDE. Here I am taking Departments table of default HR Schema to prepare the model for ADF Application.

ADF Application

Next step is to create a new project to hold unit tests so that whole application doesn’t look ambiguous. Right click on the Application name and select New–> From Gallery–> General–> Projects –> Java Project 

ADF Application

Put a name for that project and click on Finish button.

ADF Application

Now next step is to create Test Suite for business components and before that, we should know some terminology that is used in unit testing.

Test Suite– A group of Test Cases

Test Fixture– A class to handle long running test cases and keep the state of multiple test cases.

Assertation– To check the result of a test case against the expected result.

Now to open test suite wizard, Right click on new project and select New–> From Gallery–> General–> Unit Tests –> ADF Business Components Test Suite

ADF Application

Click on OK button and configure the test suite. You can see that here I have selected the Model project and DeptAm application module to test. You need to select Configuration for database connection too and here I have selected DeptAMLocal.

ADF Application

Click on the Next button and see that this wizard will generate a Test Suite class and a Test Fixture class. This wizard will also generate separate unit test classes for each view object in the application.

ADF Application

Now click on Finish button and you can under new project all files are created.

DeptAmFixture.java– Test Fixture Class

AllDeptAMTests.java– Test Suite Class

DepartmentsVO1VOTest.java– Unit Test Class for Departments ViewObject

ADF Application

Now open DepartmentsVO1VOTest.java class and look at the default test case that checks that the Department View Object should not be null.

You can see here @Test annotation, this indicates that this java method is a unit test and after performing test assert is used to verify the test result.

@Test
public void testAccess() {
    ViewObject view = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
    assertNotNull(view);
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

To check this default test case, Right click on test suite class and select run. You can see that unit test execute successfully.

ADF Application

Next step is to create some of the own unit tests, I have created this unit test that checks that the Department Id should not null in a newly created row.

@Test
public void checkDeptIdNotNull() {
    ViewObject deptVo = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
    Row newRow = deptVo.createRow();

    newRow.setAttribute("DepartmentId", 222);
    newRow.setAttribute("DepartmentName", "Testing");

    assertNotNull("DepartmentId should not be null", newRow.getAttribute("DepartmentId"));
}

So in the above code, I have created a new row in Departments view object and set 222 in Department Id. Now run this test case.

ADF Application

You can see here that test is passed successfully because Department Id is not null, That’s great. Now comment this line in the code

//newRow.setAttribute(“DepartmentId”, 222);

and run test again

ADF Application

See that test is failed with AssertionError as Department Id is null this time. This is how we can write our own unit tests to check.

Here I am writing one more test case to check whether the department is in the database or not. I am passing 1990 as department id that is not in the database.

@Test
public void findDepartment() {
    ViewObject deptVo = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
    int deptId = 1990;

    Row row[] = deptVo.findByKey(new Key(new Object[] { deptId }), 1);
    Integer count = row.length;
    //assertTrue fails when second parameter evaluates to "false"
    assertTrue("Department Not Found", count.compareTo(0) == 1);

}

Let’s see the result

ADF Application

This is how we configure and use JUnit in Oracle ADF Application for Unit Testing.

Published on Java Code Geeks with permission by Ashish Awasthi, partner at our JCG program. See the original article here: Unit testing of ADF Application using JUnit

Opinions expressed by Java Code Geeks contributors are their own.

Ashish Awasthi

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button