AVIO Consulting

AVIO’s Groovy Testing Framework

Jun 8, 2021 | MuleSoft

Most people agree that having a solid unit testing framework is a prerequisite to a viable tech stack. MuleSoft’s application testing framework, MUnit, allows developers to build automated tests for Mule integrations within the Mule XML ecosystem. If additional flexibility is needed, MuleSoft offers a large Java test ecosystem with several base classes that could be used to build powerful tests in Mule 3. Unfortunately, MuleSoft did not continue the testing ecosystem in Mule 4 so AVIO built a Mule Groovy testing framework. The tool is for developers and written by developers. It aims to improve the testing experience for the technical user more than the non-technical user.

What is it?

At its simplest, it’s lines of code that start your Mule app for you (from Maven libraries), aids in mocking, and allows invoking flows using Groovy with xUnit frameworks (e.g. JUnit).

Design Goals

  1. Favor real languages over graphical configuration files
  2. Broad test tool ecosystem
  3. No “lazy loading”, run in the JVM
  4. Minimize duplication from implementation

What value does it add in these areas?

There are not many choices in the Mule 4 ecosystem that prioritize the design goals listed above. Because of this, AVIO embraced Groovy due to its similarity to Java and progressive features. It’s always a good idea to use a language designed for writing software and not for describing data like XML/JSON. 

MUnit attempts to offer an easy loading option to run tests faster but it can be quite brittle and creates a large change between how Mule loads in normal/non-test situations vs. test situations. Here at AVIO we like to complete the app load as soon as possible so we can focus on future features like a background daemon to keep an app “test run ready” to help improve speed. 

Finally, the Groovy Framework also uses the ‘target’ value from the implementation rather than forcing your mocks to repeat what your implementation has already specified.

Here’s a Mule XML under test example: 

class HttpTest extends BaseJunitTest {
@Test
void mocksProperly() {
// arrange
def stuff = null
mockRestHttpCall('SomeSystem Call') {
json {
whenCalledWith { Map incoming ->
stuff = incoming
[reply: 456]
}
}
}

// act
def result = runFlow('restRequest') {
json {
inputPayload([foo: 123])
}
}

// assert
assertThat stuff,
is(equalTo([key: 123]))
assertThat result,
is(equalTo([reply_key: 457]))
}
}

Test Class

<!--?xml version="1.0" encoding="UTF-8"?-->









<!--[CDATA[%dw 2.0
output application/json
---
{
key: payload.foo
}]]-->





<!--[CDATA[%dw 2.0
output application/json
---
{
reply_key: payload.reply + 1
}]]-->




How Do You Get Started?

 

Currently, the framework is only available as a source and can be installed using the steps below. 

  1. Clone avioconsulting/mule-4-engine-dependency-resolver and install
  2. Clone avioconsulting/mule-groovy-testing and install
  3. Put the proper dependencies and plugins into your Mule app under test’s pom.xml
  4. Start writing tests – See the README in the mule-groovy-testing repo for more information

The framework is open-source and pull requests/feedback are always welcome.