关于单元测试的建议,要测试的类依赖于Java串行端口
我正在开发一个大型遗留java应用程序。它已经拥有一个广泛的现有框架来处理设备驱动程序。我需要为通过 JavaComm 连接到串行端口的设备创建一个新的设备驱动程序。
现有的驱动程序只需在其 configure()
方法中创建新的串行端口,然后从串行端口对象创建新的输入和输出流,然后将这些输入和输出流用于设备通信,但使用没有单元测试。
然而,我希望我的新类能够进行单元测试,但不确定如何才能做到这一点,如果它要适合这个现有的框架,该框架将期望在配置中设置串行端口、输入和输出流() 方法。
有什么想法吗?
public void configure()
{
super.configure();
if (isEmulated()) {
return;
}
if (isFaulty()) {
if (isOutOfService()) {
ZBopNotificationManager.getInstance().add(
new SystemNotice(SeverityCode.LEVEL3, getName(), getErrorMessage()));
}
return;
}
// ZP:
String portName = getSerialPort();
// String portName = "COM1";
try {
CommPortIdentifier id = CommPortIdentifier.getPortIdentifier(portName);
Trace.log(this, Trace.D, "Port name = " + id.getName());
port = (SerialPort) id.open(getName(), 1000);
} catch (NoSuchPortException nspe) {
report(SeverityCode.LEVEL3, getName(), "Bar Code Scanner is not connected to " + portName + " port, or the port does not exist.");
return;
} catch (PortInUseException piue) {
report(SeverityCode.LEVEL3, getName(), portName + " port is already in-use by some other device. Reason: " + piue.getMessage());
return;
}
try {
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
// this should not happen
port.close();
report(SeverityCode.LEVEL2, getName(), portName + " port configuration failed: " + e);
return;
}
try {
in = port.getInputStream();
out = port.getOutputStream();
} catch (IOException ioe) {
port.close();
report(SeverityCode.LEVEL3, getName(), portName + " port configuration failed: " + ioe.getMessage());
return;
}
meout(30);
// ... other init code omitted
}
I am working on a large legacy java application. It already has an extensive existing framework in place for handling device drivers. I need to create a new device driver for a device that connects to a serial port via JavaComm.
The existing drivers simply create the new serial port inside their configure()
methods, then create new input and output streams from the serial port object, then use those input and output streams for the device comms, but with no unit tests.
I want my new class to be unit testable however, but not sure how I can do that if it's going to fit in with this existing framework that will expect the serial port, input and output streams to be set up in the configure()
method.
Any ideas?
public void configure()
{
super.configure();
if (isEmulated()) {
return;
}
if (isFaulty()) {
if (isOutOfService()) {
ZBopNotificationManager.getInstance().add(
new SystemNotice(SeverityCode.LEVEL3, getName(), getErrorMessage()));
}
return;
}
// ZP:
String portName = getSerialPort();
// String portName = "COM1";
try {
CommPortIdentifier id = CommPortIdentifier.getPortIdentifier(portName);
Trace.log(this, Trace.D, "Port name = " + id.getName());
port = (SerialPort) id.open(getName(), 1000);
} catch (NoSuchPortException nspe) {
report(SeverityCode.LEVEL3, getName(), "Bar Code Scanner is not connected to " + portName + " port, or the port does not exist.");
return;
} catch (PortInUseException piue) {
report(SeverityCode.LEVEL3, getName(), portName + " port is already in-use by some other device. Reason: " + piue.getMessage());
return;
}
try {
port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
// this should not happen
port.close();
report(SeverityCode.LEVEL2, getName(), portName + " port configuration failed: " + e);
return;
}
try {
in = port.getInputStream();
out = port.getOutputStream();
} catch (IOException ioe) {
port.close();
report(SeverityCode.LEVEL3, getName(), portName + " port configuration failed: " + ioe.getMessage());
return;
}
meout(30);
// ... other init code omitted
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从表面上看,
javax.comm
API 并没有让单元测试变得轻松 - 它的类较多,而接口较少。我的建议是为您需要在驱动程序中使用的每个 javax.comm 类创建接口和适配器类。然后,您的驱动程序代码将与这些接口通信,而不是直接与 javax.comm 通信。无论如何,您可能只需要 API 的一个子集,定义这些接口应该可以帮助您阐明您的设计。
这将允许您在单元测试中使用这些接口的模拟实现(例如 JMock、Mockito 等)。您的单元测试可以将这些模拟注入到驱动程序对象中。
当实际使用时,驱动程序的
configure()
方法可以实例化适配器类而不是模拟。By the looks of things, the
javax.comm
API doesn't make life easy for unit tests - it's big on classes, and light on interfaces.My suggest would be to create interfaces and adaptor classes for each
javax.comm
class that you need to use in your driver. Your driver code would then talk to those interfaces, rather than directly tojavax.comm
. You probably only need a subset of the API anyway, and defining these interfaces should help you clarify your design.This will allow you to use mock implementations of those interfaces in your unit tests (e.g. JMock, Mockito, etc). Your unit test can inject these mocks into the driver object.
When used for real, the driver's
configure()
method can instantiate the adaptor classes rather than the mocks.如果我理解正确的话,您想测试设备驱动程序而不是使用设备驱动程序的模块。
使用集成测试而不是单元测试可以吗?如果将串行端口 rxdata 与 txdatapin 连接,将 rts 连接至 cts pin,则集成测试可以检查发送到输出流的所有内容是否应由输入流接收。
If i understood you correctly you want to test devicedriver and not a module that uses the device driver.
Is it ok to have an integrationtest instead of a unittest? If you connect the serial port-s rxdata with the txdatapin and the rts with the cts pin then the integration test can check that every thing send into outputstream should be received by the inputstream.