Unit tests with Mockito

Hello MicroEJ team,

We’re currently using your JUnit module to implement Unit Tests for our libraries (build-microej-javalib build-type) that we can run in simulation.

To ease our test implementation, we would like to use a framework such as Mockito, but this doesn’t work in the MicroEJ VM because of missing opcodes.
Since they are pure Java, these tests could be run in a standard J2SE VM.

Is there a way to select on which VM the tests can be run at build time?

Yes you can switch your tests on J2SE VM with another Easyant plugin. By default your application uses the com.is2t.easyant.plugins#microej-testsuite Easyant plugin, which executes the tests on the MicroEJ VM. There is another test plugin in the SDK to execute the tests on a J2SE VM : com.is2t.easyant.plugins#microej-junit
So you must disable the first one and enable the second one. You can do this by adding these 2 lines in your module.ivy file:

<ea:plugin org="com.is2t.easyant.plugins" module="microej-junit" revision="1.0.+"/>
<ea:property name="skip.com.is2t.easyant.plugins#microej-testsuite;[3.3.0-RC0,4.0.0-RC0[" value="true" />

Easyant requires to know the declared version of the plugin to disable it, here [3.3.0-RC0,4.0.0-RC0[ is the version of the plugin for the SDK 5.4. You can find this version in your Ivy cache for example, in the file com.is2t.easyant.buildtypes/build-microej-javalib/ants/build-microej-javalib-x.y.z.xml, on the line:

<ea:import mrid="com.is2t.easyant.plugins#microej-testsuite;{version}"/>

Then you can add junit and mockito dependencies in your module.ivy file (assuming the mockito library is available in your modules repository), for example:

<dependency org="junit" name="junit" rev="4.12" conf="test->default" />
<dependency org="org.mockito" name="mockito-core" rev="3.5.11" conf="test->default" />

and start writing your tests with Mockito.

Hope this helps.

Hello @tdelhomenie

After changing my module.ivy file, unit tests with Mockito are now working properly.

Thanks a lot for your help.