# JUnit Integration Tutorial

The Code Coverage Tool provides an additional JUnit launch configuration delegate as a means to integrate with the JUnit test framework. This allows for determining the achieved code coverage for test suites containing containing test cases for Operational QVT (QVTO) model transformations. This section explains how to measure and visualize the code coverage of a test suite. Basic knowledge about EMF, QVTO, JUnit is assumed.

Note: The example created by following the tutorial is available here.

## Steps

1. Create a QVTO transformation, e.g. from this tutorial.
2. Create a Plug-in project.
3. Add a JUnit test case to the project
4. Add and implement a test case. An example code snippet is listed below:

public class SingleTest { private static final URI TRANSFORMATION_FILE_URI = URI.createFileURI("C:/Localdata/Eclipse/qvto-coverage/runtime-ws/org.eclipse.qvto.example/transforms/HelloWorld.qvto"); private static final URI IN_MODEL_PLATFORM_URI = URI.createURI("platform:/resource/org.eclipse.qvto.example.mm.test/instances/in.abc"); private static final URI OUT_MODEL_PLATFORM_URI = URI.createURI("platform:/resource/org.eclipse.qvto.example.mm.test/instances/out.abc"); @BeforeClass static public void setUp() throws Exception { // Add project to platform resource URI map EcorePlugin.getPlatformResourceMap().put("org.eclipse.qvto.example.mm.test", URI.createFileURI(System.getProperty("user.dir") + '/')); // Register resource factory for our ABC meta-model Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("abc", new XMIResourceFactoryImpl()); // Register package AbcPackage.eINSTANCE.eClass(); } @Test public void test() { // create the input extent with its initial contents ResourceSet resourceSet = new ResourceSetImpl(); Resource inResource = resourceSet.getResource(IN_MODEL_PLATFORM_URI, true); EList<EObject> inObjects = inResource.getContents(); ModelExtent input = new BasicModelExtent(inObjects); // create an empty output model extent ModelExtent output = new BasicModelExtent(); // setup the execution environment details -> // configuration properties, logger, monitor object etc. ExecutionContextImpl context = new ExecutionContextImpl(); // run the transformation assigned to the executor TransformationExecutor executor = new TransformationExecutor(TRANSFORMATION_FILE_URI); ExecutionDiagnostic result = executor.execute(context, input, output); // In case of success, store the output objects in it's resource and persist the resource if(result.getSeverity() == Diagnostic.OK) { List<EObject> outObjects = output.getContents(); ResourceSet resourceSet2 = new ResourceSetImpl(); Resource outResource = resourceSet2.createResource(OUT_MODEL_PLATFORM_URI); outResource.getContents().addAll(outObjects); try { outResource.save(Collections.emptyMap()); } catch (IOException e) { e.printStackTrace(); } } else { // Consider the test failed fail("Failed to execute the transformation"); } } }

5. Run the JUnit test (Run As -> JUnit Test)
6. A dialog will be presented, asking to chose between the available JUnit launch delegate. This includes the one contributed by the Code Coverage Tool, which is the Operational QVT Interpreter Coverage Launcher. Either use configuration specific settings or change the workspace global setting to prefer the Operational QVT Interpreter Coverage Launcher. The former method allows ruling out the Operational QVT Interpreter Coverage Launcher in case a JUnit test suite does not include any QVT model transformations.

7. Press OK to confirm your choice. The test case will now be executed.
8. Upon successful execution of the test case, the Operational QVT Code Coverage View is instantiated and given focus, in order to directly present the coverage data.