ERP 2.50:Developers Guide/How to create testcases/it
Languages: |
Contents |
Obiettivo
This how-to will focus on creating a testcase making use of the Openbravo test base classes. The testcase will check that our system has at least one User with password. For this, we'll use the new DAL approach to access the database.
In computer programming, unit testing is a software design and development method where the programmer gains confidence that individual units of source code are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.[1]
All new developments must belong to a module that is not the core module. Please follow the How to create and package a module section to create a new module.
Creare i testcase
Per iniziare
Testcases del Core di Openbravo ERP
Openbravo ERP provides a set of unit tests, often called 'test cases' for things such as: runtime model consistency, data access layer features, webservices features, etc. This test cases are based on JUnit testing framework.
The set of tests you can find and study them in the src-test folder under your source root folder:
root_source |- build |- config |- legal |- lib |- modules |- referencedata |- src |- srcAD |- srcClient |- src-core |- src-db |- src-diagnostics |- src-gen |- src-test <-- source of the core testcases |- src-trl |- src-wad |- web |- WebContent
Here is a direct link to the testcases in the code repository: Openbravo 2.50 test cases.
Ereditare da BaseTest
All the core testcases are subclasses from BaseTest (org.openbravo.test.base.BaseTest). This class handles all the necessary steps to initialize the Data Access Layer, takes care of transaction handling and provides a set of utilities (methods) for working with the Openbravo context (OBContext).
Creare la classe Java
- Open your Eclipse IDE.
- Create a new folder structure under the modules folder: modules/org.openbravo.test.examples/src-test/org/openbravo/test/examples.
- Add the newly created src-test folder to the Java sources on the project.
- Create a new Java class on the org.openbravo.test.examples package with the following content:
package org.openbravo.test.examples; import java.util.List; import org.openbravo.dal.service.OBCriteria; import org.openbravo.dal.service.OBDal; import org.openbravo.model.ad.access.User; import org.openbravo.test.base.BaseTest; public class ExampleTest extends BaseTest { public void testUsersCount() { setSystemAdministratorContext(); final OBCriteria<User> uCriteria = OBDal.getInstance().createCriteria(User.class); final List<User> uList = uCriteria.list(); int userCount = 0; for (User u : uList) { if (u.getPassword().length() > 0) userCount++; } assertTrue(userCount > 0); System.out.println("Total of users with password: " + (userCount)); } }
Comprendere la classe
You have just created a new class named Example that extends from the BaseTest class.
public void testUsersCount() {}
This class has a testUsersCount function. Note that all testing methods must start with test in the function name. e.g. testAllWarehouses(), testMyFirstTest(), etc
setSystemAdministratorContext();
Sets the context as if a System Administrator is logged in the application.
![]() | There is a setUserContext(...) method to select running as another user, this method can be called in the middle of a testcase to switch user (if required) |
final OBCriteria<User> uCriteria = OBDal.getInstance().createCriteria(User.class); final List<User> uList = uCriteria.list();
Uses the OBDal instance to create a new OBCriteria object, and uses it for listing all (since we are not filtering) the Users in the database.
int userCount = 0; for (User u : uList) { if (u.getPassword().length() > 0) userCount++; }
We loop trough the uList collection, and we increment the userCount variable if the user has a password length > 0.
assertTrue(userCount > 0);
We assert that the userCount is more than 0. You can check the list of possible assertions in JUnit's Assert class API.
System.out.println("Total of users with password: " + (userCount));
Finally we print a total of users with password just for the record.
Transaction Handling
A question which might pop-up when looking at the above code: where is the database transaction handling done? The answer is that this is handled by the BaseTest class and the Openbravo data access layer:
- a transaction is automatically started at first database access in the test cases. This is done by the Data Access Layer.
- a transaction is either committed (when no exception happened) or rolled-back (when an exception happened).
The BaseTest class detects automatically if an exception happened or not.
There are certainly cases whereby it makes sense to have more control over the database transactions. There are a number of relevant methods which can be useful then:
- BaseTest.commitTransaction: a convenience method which commits the transaction for you. This can be useful if you want to check if certain database actions result in exceptions in the database.
- OBDal.getInstance().flush(): flushes the update/insert queries in hibernate to the database.
- OBDal.getInstance().commitAndClose(): commits the transaction and closes the session. A new session/transaction is automatically started at the next database access.
- OBDal.getInstance().rollbackAndClose(): rolls back and closes the transactions. A new session/transaction is automatically started at the next database access.
Side-Effect Free
A test case will often change the data in the underlying database. Most of the time it is not feasible to setup a completely new test database for each test run. Therefore test-cases should be developed such that they are side effect free. This means:
- When the test-case changes data then it should have a test method which is run as the last test method which cleans up/repairs the data.
- This clean-up method should also clean up data which is left from previous test runs.
This last point is important because there can be always reasons why during a test the clean-up step is not performed. For example because the test run is stopped before the clean-up is done.
Ant Test Tasks
Openbravo has a number of ant tasks which run the test cases:
- run.tests: the default, it runs the suite: org.openbravo.test.AntTaskTests. These tests are side effect free and can be run multiple times, after the run the database should be in the same state as before.
- run.quick.tests: this task runs test cases which are fast and which test the most important parts of the system. It runs the test suite: org.openbravo.test.AllQuickAntTaskTests.
- run.all.tests: runs the suite org.openbravo.test.AllAntTaskTests. This suite contains all the test cases, also tests which can change the database.
All the test cases are based on the Small Bazaar default data.
When adding new test classes to Openbravo ERP the developer has to always add the test class to the AllAntTaskTests test suite and if it is side effect free and quick to the AllQuickAntTaskTests and if it is side effect free but takes a bit more time to the AntTaskTests test suite.
Il Risultato
To be able to execute your testcases:
- Right click on the ExampleTest class.
- Select Run AS > JUnit Test (Eclipse recognizes the class as a JUnit test because it, through BaseTest, inherits from the JUnit TestCase class).
- You can check the result of the test case on the JUnit view:
- And the output of your tests in the Console view:
Note
Languages: |