vendredi 23 décembre 2016

How to write unit tests for classes which use hardware resources?

I created a class which extends from the JSSC library and which uses low level communication methods (sendByte, sendString, etc.). I wanted to test it via JUnit, but I don't quite know how to do it.

For example, let's have a look at the method below:

public void openConnection() throws SerialPortException {
  serialPort.openPort();
  configureConnectionParameters(serialPort);
  configureReadListener(serialPort);
}

To ensure the method works properly, I need an actual hardware device to see if the port opens correctly and during the configuration process there were no Exceptions thrown. But playing with external resources during the unit testing is generally considered as a bad practice, so I started wondering if there are any solutions to such a problems (mocking up a hardware maybe?).

Or, do I have to unit test it at all?

Aucun commentaire:

Enregistrer un commentaire