Unit Testing Framework

IDE Setup

Maven Dependencies (pom.xml)

The following dependencies must be added to your pom.xml file in addition to those listed in the Samples section:

<dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
</dependency>
<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
</dependency>
      

A Simple Script Example

Consider our metadata update script. This simple script will retrieve our asset’s metadata fields, update one of the field values, then set the metadata back on the asset.

       class UpdateAssetMetadata extends PluginCommand {
   def execute() {
       def asset = context.asset
       def assetService = services.assetService
       def metadata = assetService.getAssetMetadata(asset.id)
       def directorField = metadata.getField('director')
       directorField.setValue('Michael Bay')
       assetService.setAssetMetadata(asset.id, directorField)
   }
}     
    

A Simple Test Example

The unit test below will verify that our update metadata script is updating our metadata field correctly. Most unit tests for scripts which manipulate assets consist of similar steps:

1) Create an asset object

2) Add some information to the asset (in this case, create a metadata instance)

3) Associate the information with the asset using a service mock

4) Run the method under test

5) Assert that the script took effect

class UpdateAssetMetadataTest extends GroovyTestCase {
   MockContext context
   MioServiceFactory services
   @Override
   void setUp() {
       // Instantiate our context and service
       context = new MockContext()
       services = mock(MioServiceFactory.class)
   }
   void testExecute_DirectorExists() {
       // Set an asset object on the context
       def asset = new Asset(id: 123)
       context.setAsset(asset)
       // Create an initial metadata instance
       def metadata = new MockMetadata([
               new MockSingleField('director', '')
       ])
       // Tell the assetService mock to return our metadata instance
       def assetService = mock(AssetService.class)
       doReturn(metadata).when(assetService).getAssetMetadata(eq(asset.id))
       // Tell the services mock to return our assetService mock
       doReturn(assetService).when(services).getAssetService()
       // Run the method being tested
       def command = new UpdateAssetMetadata(context: context, services: services)
       command.execute()
       // Verify that our script changed the metadata field
       assertEquals('Michael Bay', metadata.getField('director').value)
   }
}
  
https://help.ooyala.com/sites/all/libraries/dita/en/media-logistics/flex/dev/54/technical_start_guide_uat_framework.html

Was this article helpful?