Executing persistent entities tests

In previous recipes we saw how to create mock and integration tests for persistent entities. In this recipe we'll look at how to execute these tests using the perform tests command of Spring Roo.

Getting ready

Exit the Roo shell and delete the contents of the C: oo-cookbookch02-recipes directory.

Execute the ch02_jsr303_fields.roo script. It creates a flight-app Roo project and sets up Hibernate as the persistence provider using the persistence setup command. The script also creates a Flight entity, which has FlightKey as its composite primary key class, and adds fields to the Flight and FlightKey classes. If you are using a different database than MySQL or your connection settings are different from what is specified in the script, then modify the script accordingly.

Install the MySQL 5.5.11 database—this is required because we'll now be executing integration tests. Create a database named "myFlightAppDB" in MySQL server instance and ensure that the connection properties defined in the database.properties file of the flight-app Roo project can be used to successfully connect to "myFlightAppDB".

Start the Roo shell from the C: oo-cookbookch02-recipes directory.

How to do it...

Follow these steps to create and execute tests:

  1. Change the focus of Roo commands to Flight entity:
    roo> focus --class ~.domain.Flight
    
  2. Execute the test integration command to create integration tests for the Flight entity:
    ~.domain.Flight> test integration
    
  3. To execute tests defined in the Roo project, issue the perform tests command from the Roo prompt, as shown here:
    roo> perform tests
    

The integration test for the Flight entity will fail currently because:

  • The auto-generated 'data on demand' class provides limited support for creating entity instances that comply with JSR 303 annotations. As explained in the Creating integration tests for persistent entities recipe, you can write custom setter methods for persistent fields in the FlightDataOnDemand.java entity to create Flight entities that comply with JSR 303 annotations specified on the persistent fields.
  • The query fired by the countFlights method in the Flight_Roo_Entity.aj AspectJ ITD file doesn't work with MySQL 5.1.1. You need to change the countFlights method from:
    public static long countFlights() {
       return entityManager().createQuery("SELECT COUNT(o) 
         FROM Flight o", Long.class).getSingleResult();
    }

    To:

    public static long countFlights() {
       return entityManager().createQuery("SELECT COUNT(*) 
         FROM Flight o", Long.class).getSingleResult();
    }

As AspectJ ITD files are managed by Spring Roo, you should not change the countFlights method in the Flight_Roo_Entity.aj file. Instead, either perform push-in refactoring or create a countFlights method in the Flight.java file.

In push-in refactoring, you use the IDE to push the declarations, methods, attributes, and constructors defined in the AspectJ ITD file to the target Java class. For more information on push-in refactoring, refer to the Removing Roo with push-in refactoring recipe of Chapter 7.

Note

When writing countFlights method in Flight.java, make sure that Roo is running in the background, so that Roo can remove the countFlights method from the Flight_Roo_Entity.aj file. Additionally, ensure that the signature of countFlights method is same as the one defined in the Flight_Roo_Entity.aj file.

How it works...

The perform tests command is processed by the maven add-on of Roo and is responsible for executing all the tests that form part of the Roo project. If you want to directly execute the tests using maven, then exit the Roo prompt and use the mvn test command of maven. The results of test execution are saved in the <project_dir>/target/surefire-reports directory, where project_dir is your Roo project directory.

There's more...

The ch02_flightapp_testing.zip file that accompanies this book contains the flight-app Eclipse project that we saw in recipes of the previous chapter and this chapter. You can import this project into your Eclipse IDE and view the following changes that I made to the flight-app project to demonstrate concepts that we've learned so far:

  • Defined the setNumOfSeats method in FlightDataOnDemand.java to create Flight entities that meet the constraint specified by the JSR 303 @Size annotation for the numOfSeats field. Defining the setNumOfSeats method in FlightDataOnDemand.java results in the removal of the method with the same signature from the FlightDataOnDemand_Roo_DataOnDemand.aj file.
  • Defined setOrigin and setDestination methods in FlightDataOnDemand.java to create Flight entities that meet the constraints specified by JSR 303 @DecimalMax and @DecimalMin annotations on origin and destination fields. Defining setOrigin and setDestination methods in FlightDataOnDemand.java results in removal of methods with the same signature from the FlightDataOnDemand_Roo_DataOnDemand.aj file.
  • Changed the @RooIntegrationTest annotation in FlightIntegrationTest.java to instruct Roo not to generate findAll, findEntries, and count test methods.
  • Autowired MyFlightDod reference (a custom 'data on demand' class) in FlightIntegrationTest.java and defined a custom testMyCustomDodTest method.
  • Added a @Rollback(false) Spring annotation to the testPersist method in FlightIntegrationTest.java so that the data created by the testPersist method is not rolled back after it is executed. This feature could be particularly useful if you want to manually verify the data that was created by the testPersist method or if you want to keep the data for further testing.

You can execute the tests defined in the flight-app project using the perform tests command and check your database to view the seed data that was created by the 'data on demand' class and during the testing of testPersist method.

See also

  • Refer to the Creating integration tests for persistent entities recipe to see Spring Roo's support for auto-generation of integration tests for JPA entities
  • Refer to the Creating mock tests for persistent entities recipe to mock static methods defined in entities
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.52.188