Repository navigation
Better unit tests #37
Copy link
Copy link
Open
Labels
clean-upcomponent:mavenChanges to the java build processChanges to the java build processgood first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is neededlanguage:javaNeeds changes to java sources.Needs changes to java sources.
Milestone
Description
A placeholder to port over some of @vogt31337's unit tests from the 2.8.1-experimental branch.
public void testSerialPortListing() {
String[] out = SerialPortList.getPortNames();
System.out.println(Arrays.toString(out));
// if we reached this point, loading the dynamic lib worked.
assertTrue(true);
}
private String findASerialPort() {
String[] out = SerialPortList.getPortNames();
if (out.length > 0) {
return out[0];
} else {
return null;
}
}
public void testSerialPortRead() {
String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux
if (port != null) {
SerialPort serialPort = new SerialPort(port);
try {
serialPort.openPort();//Open serial port
serialPort.setParams(9600, 8, 1, 0);//Set params.
byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
serialPort.closePort();//Close serial port
} catch (SerialPortException ex) {
System.out.println(ex);
}
} else {
Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?");
}
assertTrue(true);
}
private String findASerialPort() {
String[] out = SerialPortList.getPortNames();
if (out.length > 0) {
return out[0];
} else {
return null;
}
}
public void testSerialPortWrite() {
String port = findASerialPort(); // could also be "COM1" under windows, or /dev/ttyS0 under linux
if (port != null) {
SerialPort serialPort = new SerialPort(port);
try {
serialPort.openPort();//Open serial port
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
serialPort.writeBytes("This is a test string".getBytes());//Write data to port
serialPort.closePort();//Close serial port
} catch (SerialPortException ex) {
System.out.println(ex);
}
} else {
Assert.fail("No comport found. Maybe running on a laptop? Or cables aren't attached?");
}
assertTrue(true);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
clean-upcomponent:mavenChanges to the java build processChanges to the java build processgood first issueGood for newcomersGood for newcomershelp wantedExtra attention is neededExtra attention is neededlanguage:javaNeeds changes to java sources.Needs changes to java sources.
Activity
bmarwell commentedon Apr 14, 2019
To get a stable unit test, we might want to create a virtual serial port using a
@Beforedirective.The setup might differ a little per Operating System. Another good option would be to use a junit
@Rule. For Linux, this wouldusesocat, other methods for other OSes.tresf commentedon Apr 16, 2019
I vote we skip virtual com ports for Windows. They generally require signed drivers and get into a bit of a mess. If a CI service such as AppVeyor has support for them, this would allow us to do CI testing, but local testing on a PC would still need to make some assumptions.
bmarwell commentedon Apr 18, 2019
That's okay for me! :)
I will start with linux for now.
Add basic read/write unit tests using socat
tresf commentedon Nov 10, 2023
Partially implemented via #47, #151.
#151 was particularly interesting to figure out, mostly due to some test running against old binaries. This should all be sorted now.