Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Unit Testing. Show all posts
Showing posts with label Unit Testing. Show all posts

Monday, 21 January 2019

Unit testing of ADF Application using JUnit

 JUnit is a unit testing package for Java language and can be used to test Oracle ADF applications 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 how can we set up and use JUnit in JDeveloper 12.2.1.3 to test the ADF application.

JDeveloper 12.2.1.3 comes with a 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.




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



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



Now next step is to create a 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 the test suite wizard, Right-click on new project and select New–> From Gallery–> General–> Unit Tests --> ADF Business Components Test Suite



Click on the 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.



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.



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



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.

  1. @Test
  2. public void testAccess() {
  3. ViewObject view = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
  4. assertNotNull(view);
  5. }
  6. @Before
  7. public void setUp() {
  8. }
  9. @After
  10. public void tearDown() {
  11. }

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



The 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 be null in a newly created row.

  1. @Test
  2. public void checkDeptIdNotNull() {
  3. ViewObject deptVo = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
  4. Row newRow = deptVo.createRow();
  5. newRow.setAttribute("DepartmentId", 222);
  6. newRow.setAttribute("DepartmentName", "Testing");
  7. assertNotNull("DepartmentId should not be null", newRow.getAttribute("DepartmentId"));
  8. }

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



You can see here that the 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 the test again



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.

  1. @Test
  2. public void findDepartment() {
  3. ViewObject deptVo = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
  4. int deptId = 1990;
  5. Row row[] = deptVo.findByKey(new Key(new Object[] { deptId }), 1);
  6. Integer count = row.length;
  7. //assertTrue fails when second parameter evaluates to "false"
  8. assertTrue("Department Not Found", count.compareTo(0) == 1);
  9. }

Let's see the result



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

Cheers 🙂 Happy Learning