Pular para o conteúdo

Testando com JUnit no ADF: Como testar a sua aplicação ADF utilizando JUnit

Testando com JUnit no ADF

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.

Testando com JUnit no ADF

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.

y3drWMwu5ToQ6YLIa ezIlBklJoe1a4yjG28xUDz3gJ9f7ARaTQWsNAKNeo3DeOGdqkVyXHXgekfl5HzXWvnihnG8 XYvwweqBrJR 1vyoMwOmw9yBpXKXYs5AvFiFacD

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;

  }

}
o5J vYkyBXd9jnCThiVHI 3slPS596Nva4ppsF5E2iU31GSXF CzUjqOuFmGz7 LZpShQdpCvlEoH223JS687bw06U5XiWB kHeOyMhWccLmgQEpBPoX3mSoaGeWbnRZWwjR4Fc9nrc

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.

V guXm PqvKd5zm7AVkL8agMKfluaRF

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.

K560xsajXhLwArcSxwObdjBTRBXWByg72BO2CX22R2LCErBJTv8sf33srYGUh4F45b3c2Y7387nQxeN1T14O2iWZJvezsAQybaayOcEROar11tXk00qEvxcl7dZKfpDFHphWBbEd dc

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;

  }

}
rB0tmIYJYeJCueZk Q NlRvJRYS5x1JfG2yEXPR1l0mjU9CKkNTYFH6qSB0dH djqwwiBC8BFo HekrRT9BCAg0T1luurq64zq

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.

CekcspO3lhQw1wTwzApdutFQg12BC7kEBebo

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

PJ0XN oAcLTZEYAiR7N4kGpD3I3BFBUUth4zkg 1yZ6odDDT1qyy b2cJsZLfCMiXbHIji0aa6oL954SRnYolEbFo3937Dfaead5t XZcZCvvJzW9BP0

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.

yiEhrLCqIBXbyVHVt1aXJ7eCmhTMYA9nlFTclwSw0M8XiBdnvXMroopSCaTygPeRrgZFrWC0ZcZxDtZGRd60FNYhg8gaJ12E lYhspf71ZGAiWYnJS4

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

exmO1y87Ivm3aphq4 hTxake0sv7CExgdEln8AZ hi oBgL5fhadlWosO2b5qFoWtbOgomLILwNn3yoejo1DiSPILJacrptd IpH0OiOeLxe AUMZK6mPbpSHiIPk6n zF8IZYTh9hA

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);

  }

}
Fnj qoH8

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.

sLfRc0AM5LsSuCWllbS3bBkrsAHqRzKxLyAIP0VfIHxFxQxYDQlVR JsiSDAsJbfRMKujJ2wBjlCi8z348jLG4X6 YJFocDces57tBQkabYg2N236bj69Q40Yg 8wyIT7jX8uSGC24o

Quão útil foi este post ?

Clique em uma estrela para classificar o post

nota média 5 / 5. Contagem de votos: 14

Sem votos ! Seja o primeiro a classificar !

Deixe um comentário

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

plugins premium WordPress