rproell - 2015-06-21

I like to unit test my code. So when i started with Arduino i searched for a unit test framework.
I am not familiar with C and C++ so i didn't understood the examples i found.
Than i found this great haikuVM project. Where i can use my Java knowledge for Arduino.

With version 1.3.2 a Java wrapper for some Arduino C functions was added. e.g.: Arduino.java
I have build the bootstrapEmulator project. If you add this project to your classpath you can mock away the C part of the wrapper implementation. And do unit testing in a JavaVM without uploading the code to the Arduino hardware.

example code:

// create mock for arduino functions
ArduinoImpl arduinoMock = Mockito.mock([ArduinoImpl.class](https://sourceforge.net/p/haiku-vm/code/HEAD/tree/tags/HaikuVM-1.3.2/bootstrap/src/main/java/haiku/arduino/processing/ArduinoImpl.java));
Mockito.when(arduinoMock.digitalRead((byte) 1)).thenReturn(HIGH);
// register arduino mock
ArduinoEmulator.setArduinoTestDouble(arduinoMock);
// or if you want to see the arduino function calls in the console
// then you can wrap the mock with the ConsoleTestDouble
ArduinoEmulator.setArduinoTestDouble(
    ConsoleTestDouble.newInstance(arduinoMock));

// use mocked arduino functions
// in any JavaVM without uploading the code to the Arduino
pinMode((byte) 1, INPUT);
byte bit = digitalRead((byte) 1);

// verify test results
Mockito.verify(arduinoMock).pinMode((byte) 1, INPUT);
assertThat(bit, equalTo(HIGH));
// unregister arduino mock
ArduinoEmulator.clearArduinoTestDouble();

Maybe this is also useful for somebody else.

 

Last edit: rproell 2015-06-21