Creating the "Create a test case" command:

  1. Go to Junit dev module commands interfacecontainer16 "gui" / image "commands"

  2. Right-click on Junit dev module commands commandcontainer16 "commands", and select image "Modelio Studio" –> image "Command"

  3. In the "Command identification" dialog box:

  4. Enter "Create a test case" as label

  5. Tooltip: leave blank

  6. Id: leave the pre-filled value

  7. Click Next

  8. In the "Command behavior" dialog box:

  9. Action: Select "Execute a java command"

  10. Java class: Enter the following text: "CreateTestCase"

  11. Parameters : leave blank

  12. Can be applied on : Click on the Junit dev module commands plus button. A new entry appears with "ModelElement" as default value.

  13. Change the value to "Class".

Junit dev module commands JUnit CreateTestCase command

After creating the command, the "CreateTestCase" class is created in:
Junit dev module commands implementationcontainer16 implementation / image java / image org.sample.junit / image handlers / Junit dev module commands JavaPackage commands / Junit dev module commands JavaClass CreateTestCase

Adding the command to the contextual menu

  1. Go to Junit dev module commands interfacecontainer16 "gui" / image "contextual menu"

  2. Double clic on Junit dev module commands contextualmenu16 "contextual menu"

  3. Go to the Modelio Studio tab.

  4. In the left-hand list, select the "Create a test case" command

  5. Click on the Junit dev module commands arrow right button
    Junit dev module commands JUnit form add command

  6. A green check mark (Junit dev module commands checkmark16) appears next to the "Create a test case" command in the left-hand list.

Writing command method

The next thing we have to do now is to complete the methods actionPerformed() and accept() with their implementation code.

Following the steps to complete the methods:

  1. Select the following class:
    image implementation / image java / image org.sample.junit / image handlers / image commands / image CreateTestCase

  2. In the Java Property view tab, run the generate Java source (1) and edit (2)
    image

  3. Update the accept method.
    The accept() method is called to validate the availability of the command in the current selection context. The current selection context is simply passed as the list of the currently selected elements.
    For our example, let’s consider that the command accepts only one selected element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    public class CreateTestCase extends DefaultModuleCommandHandler {
    ...
        public boolean accept(List<MObject> selectedElements, IModule module) {
            // Generated call to the super method will check the scope conditions defined in Studio.
            // Do not remove this call unless you need to take full control on the checks to be carried out.
            // However you can safely extends the checked conditions by adding custom code.
            if (super.accept(selectedElements, module) == false) {
                return false;
            }
            // Check that there is only one selected element
            return selectedElements.size() == 1;
        }
    ...
    }
  1. Update the actionPerformed method.
    The actionPerformed() method is called when the command is activated by the end-user. Its role is to perform the proper action. Thus, the code of this method is the action itself.

1
2
3
4
5
6
7
8
9
    public class CreateTestCase extends DefaultModuleCommandHandler {
    ...
        public void actionPerformed(List<MObject> selectedElements, IModule module) {
            Class classToTest = (Class) selectedElements.get(0);
            TestCaseCreator testCaseCreator = new TestCaseCreator();
            testCaseCreator.createTestCase(classToTest, module);
        }
    ...
    }
  • Add the following import:

1
import org.modelio.metamodel.uml.statik.Class;

<< Creating module stereotypes

Index

Processing commands: model transformation >>