Fixtures

What are Fixtures?

A fixture is an object that inserts, or creates, data into one or more database tables before a unit test is run. At the end of the test, the fixture data is removed.

A test method declares that is requires a Fixture when it is defined, using the fixture attribute, supplying the name of a fixture class method as the attribute value.

A class method is defined in the test case, with the @Fixture annotation, with an optional name attribute. This method returns an object of type OEUnit.Data.Fixture which will contain the data to be loaded into a database when required.

How to use a Fixture

1. Define a test method, using the fixture attribute to specify the name of a class method which will provide a fixture object.
For example:

 ROUTINE-LEVEL ON ERROR UNDO, THROW.

 USING OEUnit.Assertion.Assert.
 USING OEUnit.Data.Fixture.

 CLASS SimpleTest:
   
   @Test (fixture=OrderStatusFixture). 
   METHOD PUBLIC VOID AcceptStatusChange(): 
     DEFINE VARIABLE result AS LOGICAL NO-UNDO.
     FIND FIRST Order 
          WHERE Order.Status = "NEW" NO-LOCK NO-ERROR.
     Order:OpenOrder(Order.OrderNo).
     result = Order:ChangeStatus(INPUT "ACCEPTED"). 
     Assert:AreEqual(result,TRUE).
   END METHOD.    

 END CLASS.


2. Add a method to the class which returns an OEUnit.Data.Fixture object.
Use the @Fixture annotation to indicate that the class is a Fixture method.
For example:

   @Fixture. 
   METHOD PUBLIC Fixture OrderStatusFixture(): 
     DEFINE VARIABLE fixture AS Fixture NO-UNDO.
     fixture = NEW Fixture().
     fixture:FromJSON("~{ ~"Order~": ["
                      + "~{ ~"OrderNo~": ~"10000~", ~"CustomerNo~": 99000, ~"Status~": ~"COMPLETED~"},"
                      + "~{ ~"OrderNo~": ~"10001~", ~"CustomerNo~": 99001, ~"Status~": ~"CANCELLED~"},"
                      + "~{ ~"OrderNo~": ~"10002~", ~"CustomerNo~": 99002, ~"Status~": ~"COMPLETED~"},"
                      + "~{ ~"OrderNo~": ~"10003~", ~"CustomerNo~": 99000, ~"Status~": ~"NEW~"},
                      + "~{ ~"OrderNo~": ~"10004~", ~"CustomerNo~": 99003, ~"Status~": ~"ACCEPTED~"},"
                      + "~{ ~"OrderNo~": ~"10005~", ~"CustomerNo~": 99004, ~"Status~": ~"PAID~"},"
                      + "]}").
     RETURN fixture.
   END METHOD.


Important Notes:


3. Run the test case as per normal.

Name Attribute

Each method annotated with @Fixture can specify a name attribute which is used when searching for a Fixture method, instead of the method name.

Syntax:

   @Fixture[(name="FixtureName")].

FixtureName
    The Name of the Fixture. This name can then be specified by a test method as its Fixture, in the fixture attribute


Example:

   @Test (dataProvider=OrderStatusFixture). 
   METHOD PUBLIC VOID AcceptStatusChange(): 
     DEFINE VARIABLE result AS LOGICAL NO-UNDO.
     FIND FIRST Order 
          WHERE Order.Status = "NEW" NO-LOCK NO-ERROR.
     Order:OpenOrder(Order.OrderNo).
     result = Order:ChangeStatus(INPUT "ACCEPTED"). 
     Assert:AreEqual(result,TRUE).
   END METHOD. 
   
   @Fixture (name=OrderStatusFixture). 
   METHOD PUBLIC Fixture myFixture(): 
     DEFINE VARIABLE fixture AS Fixture NO-UNDO.
     fixture = NEW Fixture().
     fixture:FromJSON("~{ ~"Order~": ["
                      + "~{ ~"OrderNo~": ~"10000~", ~"CustomerNo~": 99000, ~"Status~": ~"COMPLETED~"},"
                      + "~{ ~"OrderNo~": ~"10001~", ~"CustomerNo~": 99001, ~"Status~": ~"CANCELLED~"},"
                      + "~{ ~"OrderNo~": ~"10002~", ~"CustomerNo~": 99002, ~"Status~": ~"COMPLETED~"},"
                      + "~{ ~"OrderNo~": ~"10003~", ~"CustomerNo~": 99000, ~"Status~": ~"NEW~"},"
                      + "~{ ~"OrderNo~": ~"10004~", ~"CustomerNo~": 99003, ~"Status~": ~"ACCEPTED~"},"
                      + "~{ ~"OrderNo~": ~"10005~", ~"CustomerNo~": 99004, ~"Status~": ~"PAID~"},"
                      + "]}").
     RETURN fixture.
   END METHOD.

Fixture Methods

The Fixture class provides a range of methods for loading data into the Fixture.

Each successive call to one of these methods defines a range of data to be loaded. When the records are loaded, they are done so in the order that they are specified to the Fixture object.

METHOD PUBLIC LOGICAL FromJSON(INPUT json AS LONGCHAR)

This method is used to load data from JSON text stored in a LONGCHAR.


Notes:

METHOD PUBLIC LOGICAL FromJSONFile(INPUT path AS CHARACTER):

This method is used to load data from JSON text stored in a file. The file path should be provided as the INPUT parameter.


Notes:

METHOD PUBLIC LOGICAL FromXML(INPUT xml AS LONGCHAR)

This method is used to load data from XML data stored in a LONGCHAR.


Notes:

METHOD PUBLIC LOGICAL FromXMLFile(INPUT path AS CHARACTER):

This method is used to load data from XML data stored in a file. The file path should be provided as the INPUT parameter.


Notes:

METHOD PUBLIC LOGICAL FromTempTable(INPUT ttSrc AS HANDLE):

This method is used to load data from an existing temp-table, via the provided HANDLE INPUT parameter.


Notes:

METHOD PUBLIC LOGICAL FromDataSet(INPUT dsSrc AS HANDLE):

This method is used to load data from an existing DataSet, via the provided HANDLE INPUT parameter.


Notes: