title: How to write unit tests for classes which use hardware resources?
body: <p>I created a class which extends from the <a href="https://github.com/scream3r/java-simple-serial-connector" rel="nofollow noreferrer">JSSC library</a> 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.</p>
  
  <p>For example, let's have a look at the method below:</p>
  
  <pre><code>public void openConnection() throws SerialPortException {
    serialPort.openPort();
    configureConnectionParameters(serialPort);
    configureReadListener(serialPort);
  }
  </code></pre>
  
  <p>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?).</p>
  
  <p>Or, do I have to unit test it at all?</p>