Testando com JUnit no ADF

Tweet about this on TwitterShare on Facebook0Share on LinkedIn4Share on Google+0

Quando você está codificando, certamente você terá que testar o código para garantir que novos recursos, correções de erros e refatorações não desestabilizem a sua aplicação. O JDeveloper fornece a capacidade de gerar JUnit test case, test fixture e test suite para testar a sua aplicação ADF.

Neste post, vou criar um JUnit Test Case para testar um método de meu módulo da aplicação.
Faça o download do aplicativo de amostra: ADFJUnitApp.zip.

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.

testing-with-junit-in-adf1

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.

testing-with-junit-in-adf2

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) {
      return 
          "Employee: " + employee.getLastName() + ", " + employee.getFirstName() + 
          ", Manager: " + employee.getLastName() + ", " + employee.getFirstName();
    } else {
      return "Unassigned";
    }
  } else {
    return null;
  }
}

testing-with-junit-in-adf3

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.

testing-with-junit-in-adf4

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 br.com.waslleysouza.test, and click OK.

testing-with-junit-in-adf5

Replace the content of AppModuleFixture class with the following code.

package br.com.waslleysouza.model.test;

import br.com.waslleysouza.model.AppModuleImpl;

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 = "br.com.waslleysouza.model.AppModule";
    String config = "AppModuleLocal";
    applicationModule = Configuration.createRootApplicationModule(amDef, config);
    appModuleImpl = (AppModuleImpl) applicationModule;
  }

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

  public AppModuleImpl getAppModuleImpl() {
    return appModuleImpl;
  }
}

testing-with-junit-in-adf6

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.

testing-with-junit-in-adf7

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

testing-with-junit-in-adf8

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.

testing-with-junit-in-adf9

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

testing-with-junit-in-adf10

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

package br.com.waslleysouza.model.test;

import br.com.waslleysouza.model.AppModuleImpl;

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() {}

  @Before
  public void setUp() throws Exception {
    fixture1.setUp();
  }

  @After
  public void tearDown() throws Exception {
    fixture1.tearDown();
  }

  /**
   * @see br.com.waslleysouza.model.AppModuleImpl#findEmployeeAndManager(Integer)
   */
  @Test
  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);
  }
}

testing-with-junit-in-adf11

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.

testing-with-junit-in-adf12

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.

testing-with-junit-in-adf13

Tweet about this on TwitterShare on Facebook0Share on LinkedIn4Share on Google+0

Autor: Waslley Souza

Consultor Oracle com foco em tecnologias Oracle Fusion Middleware e SOA. Certificado Oracle WebCenter Portal, Oracle ADF e Java.

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *