Background

Creating unit tests (SysTest + ATL) and automating their running in an Azure pipeline is part of a good continuous update strategy which reduces the risk of taking frequent product updates for Dynamics 365 Supply Chain Management from Microsoft.  The guidance on Docs is fairly good, when combined with the samples shipped in a “develop” environment deployment.  The docs and samples however do not mention how to handle multiple legal entities.

By extending the SysTestCase class you ensure the data created by setup() and setupTestCase() methods are automatically rolled back after the test execution.  By default this does not create even a single legal entity… many of my tests run in the familiar DAT company.  “DAT is where it’s at!

Test Case with Multiple Legal Entities

The SysTestCaseCompanyData class provides three legal entities which can be used for testing.  These methods are used for ease:

this.primaryCompany()
this.secondaryCompany()
this.otherCompany()

The first time the method is called the legal entity will be created.  Therefore it is important to extend SysTestCaseCompanyData instead of just SysTestCase.

class DAGInventoryServiceTests extends SysTestCaseCompanyData

To make life easier, change company from DAT to the primary company permanently to prevent wrapping the entire contents of test methods in changecompany(this.primaryCompany()) { }.  Add this line of code as the first line in the setUp() method.

appl.setDefaultCompany(this.primaryCompany(), false);

Parameters and setup data must be created in both legal entities to support the test.  While doing that, create customers and vendors.

// Parameter and setup data creation in primary LE

custAccountCustomer = data.cust().customers().default().record();
vendAccountPrimary = data.vend().vendors().default().record();

changecompany(this.secondaryCompany())
{
// Parameter and setup data creation in secondary LE

// Create intercompany setup and customers
custAccountIntercompany = data.cust().customers().default().record();
vendAccountIntercompany = data.vend().vendors().default().record();

}

This is where you’ll notice the incredible power of the Acceptance Test Library (ATL) which Microsoft made generally available in Summer of 2019.  The more I get to know the class structure the more I want to expand it to cover custom functionality as well.  This statement will create the intercompany records and relationship necessary to link both legal entities and enable intercompany tests for that vendor.

data.purch().intercompanySetup().createInterCompanyTradingRelation(custAccountIntercompany.AccountNum, this.secondaryCompany(), vendAccountPrimary.AccountNum, this.primaryCompany());

This is a good way to organize the setup of the data required for the tests.

public void setUp()
{
super();

this.setupAtlNavigation();

this.setupPrimaryCompany();

changecompany(this.secondaryCompany())
{
this.setupSecondaryCompany();
}

data.purch().intercompanySetup().createInterCompanyTradingRelation(secondaryCustTable.AccountNum, this.secondaryCompany(), primaryVendTable.AccountNum, this.primaryCompany());
}