Yis*_*hai 16 java tdd swing unit-testing swingworker
考虑以下代码:
public void actionPerformed(ActionEvent e) {
setEnabled(false);
new SwingWorker<File, Void>() {
private String location = url.getText();
@Override
protected File doInBackground() throws Exception {
File file = new File("out.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
creator.write(location, writer);
} finally {
if (writer != null) {
writer.close();
}
}
return file;
}
@Override
protected void done() {
setEnabled(true);
try {
File file = get();
JOptionPane.showMessageDialog(FileInputFrame.this,
"File has been retrieved and saved to:\n"
+ file.getAbsolutePath());
Desktop.getDesktop().open(file);
} catch (InterruptedException ex) {
logger.log(Level.INFO, "Thread interupted, process aborting.", ex);
Thread.currentThread().interrupt();
} catch (ExecutionException ex) {
Throwable cause = ex.getCause() == null ? ex : ex.getCause();
logger.log(Level.SEVERE, "An exception occurred that was "
+ "not supposed to happen.", cause);
JOptionPane.showMessageDialog(FileInputFrame.this, "Error: "
+ cause.getClass().getSimpleName() + " "
+ cause.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException ex) {
logger.log(Level.INFO, "Unable to open file for viewing.", ex);
}
}
}.execute();
Run Code Online (Sandbox Code Playgroud)
url是一个JTextField,'creator'是一个用于编写文件的注入接口(因此该部分正在测试中).写入文件的位置是故意硬编码的,因为这是一个例子.而java.util.logging仅用于避免外部依赖.
您如何将其组合在一起以使其可单元测试(包括在需要时放弃SwingWorker,但随后更换其功能,至少在此处使用).
我看待它的方式,doInBackground基本上没问题.基本的机制是创建一个作家并关闭它,这几乎太简单了,无法测试,真正的工作正在测试中.但是,done方法引用有问题,包括它与actionPerformed方法耦合父类并协调启用和禁用按钮.
然而,拉开它并不明显.注入某种SwingWorkerFactory会使得捕获GUI字段变得更加难以维护(很难看出它是如何改进设计的).JOpitonPane和桌面具有Singletons的所有"优点",异常处理使得无法轻松包装get.
那么将这些代码置于测试中会有什么好处?
Dev*_*ler 10
恕我直言,这对于匿名课来说很复杂.我的方法是将匿名类重构为这样的东西:
public class FileWriterWorker extends SwingWorker<File, Void> {
private final String location;
private final Response target;
private final Object creator;
public FileWriterWorker(Object creator, String location, Response target) {
this.creator = creator;
this.location = location;
this.target = target;
}
@Override
protected File doInBackground() throws Exception {
File file = new File("out.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
creator.write(location, writer);
}
finally {
if (writer != null) {
writer.close();
}
}
return file;
}
@Override
protected void done() {
try {
File file = get();
target.success(file);
}
catch (InterruptedException ex) {
target.failure(new BackgroundException(ex));
}
catch (ExecutionException ex) {
target.failure(new BackgroundException(ex));
}
}
public interface Response {
void success(File f);
void failure(BackgroundException ex);
}
public class BackgroundException extends Exception {
public BackgroundException(Throwable cause) {
super(cause);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这允许独立于GUI测试文件写入功能
然后,actionPerformed变成这样的事情:
public void actionPerformed(ActionEvent e) {
setEnabled(false);
Object creator;
new FileWriterWorker(creator, url.getText(), new FileWriterWorker.Response() {
@Override
public void failure(FileWriterWorker.BackgroundException ex) {
setEnabled(true);
Throwable bgCause = ex.getCause();
if (bgCause instanceof InterruptedException) {
logger.log(Level.INFO, "Thread interupted, process aborting.", bgCause);
Thread.currentThread().interrupt();
}
else if (cause instanceof ExecutionException) {
Throwable cause = bgCause.getCause() == null ? bgCause : bgCause.getCause();
logger.log(Level.SEVERE, "An exception occurred that was "
+ "not supposed to happen.", cause);
JOptionPane.showMessageDialog(FileInputFrame.this, "Error: "
+ cause.getClass().getSimpleName() + " "
+ cause.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
@Override
public void success(File f) {
setEnabled(true);
JOptionPane.showMessageDialog(FileInputFrame.this,
"File has been retrieved and saved to:\n"
+ file.getAbsolutePath());
try {
Desktop.getDesktop().open(file);
}
catch (IOException iOException) {
logger.log(Level.INFO, "Unable to open file for viewing.", ex);
}
}
}).execute();
}
Run Code Online (Sandbox Code Playgroud)
此外,FileWriterWorker.Response可以将实例分配给变量并独立测试FileWriterWorker.
当前的实现将线程问题,UI和文件编写结合在一起 - 正如您发现耦合使得单独测试单个组件变得困难.
这是一个相当长的响应,但它归结为将当前实现中的这三个问题从具有已定义接口的单独类中拉出.
分解应用逻辑
首先,关注核心应用程序逻辑并将其移动到单独的类/接口中.界面允许更容易模拟,并使用其他摆动线程框架.分离意味着您可以完全独立于其他问题测试您的应用程序逻辑.
interface FileWriter
{
void writeFile(File outputFile, String location, Creator creator)
throws IOException;
// you could also create your own exception type to avoid the checked exception.
// a request object allows all the params to be encapsulated in one object.
// this makes chaining services easier. See later.
void writeFile(FileWriteRequest writeRequest);
}
class FileWriteRequest
{
File outputFile;
String location;
Creator creator;
// constructor, getters etc..
}
class DefualtFileWriter implements FileWriter
{
// this is basically the code from doInBackground()
public File writeFile(File outputFile, String location, Creator creator)
throws IOException
{
Writer writer = null;
try {
writer = new FileWriter(outputFile);
creator.write(location, writer);
} finally {
if (writer != null) {
writer.close();
}
}
return file;
}
public void writeFile(FileWriterRequest request) {
writeFile(request.outputFile, request.location, request.creator);
}
}
Run Code Online (Sandbox Code Playgroud)
分开UI
现在应用程序逻辑分离,然后我们将成功和错误处理分解出来.这意味着可以在不实际执行文件写入的情况下测试UI.特别是,可以测试错误处理,而不需要引发那些错误.在这里,错误很简单,但通常很难引起一些错误.通过分离错误处理,还可以重用或替换错误处理方式.例如,稍后使用JXErrorPane.
interface FileWriterHandler {
void done();
void handleFileWritten(File file);
void handleFileWriteError(Throwable t);
}
class FileWriterJOptionPaneOpenDesktopHandler implements FileWriterHandler
{
private JFrame owner;
private JComponent enableMe;
public void done() { enableMe.setEnabled(true); }
public void handleFileWritten(File file) {
try {
JOptionPane.showMessageDialog(owner,
"File has been retrieved and saved to:\n"
+ file.getAbsolutePath());
Desktop.getDesktop().open(file);
}
catch (IOException ex) {
handleDesktopOpenError(ex);
}
}
public void handleDesktopOpenError(IOException ex) {
logger.log(Level.INFO, "Unable to open file for viewing.", ex);
}
public void handleFileWriteError(Throwable t) {
if (t instanceof InterruptedException) {
logger.log(Level.INFO, "Thread interupted, process aborting.", ex);
// no point interrupting the EDT thread
}
else if (t instanceof ExecutionException) {
Throwable cause = ex.getCause() == null ? ex : ex.getCause();
handleGeneralError(cause);
}
else
handleGeneralError(t);
}
public void handleGeneralError(Throwable cause) {
logger.log(Level.SEVERE, "An exception occurred that was "
+ "not supposed to happen.", cause);
JOptionPane.showMessageDialog(owner, "Error: "
+ cause.getClass().getSimpleName() + " "
+ cause.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
Run Code Online (Sandbox Code Playgroud)
分开线程
最后,我们还可以使用FileWriterService分离线程问题.使用上面的FileWriteRequest可以使编码变得更简单.
interface FileWriterService
{
// rather than have separate parms for file writing, it is
void handleWriteRequest(FileWriteRequest request, FileWriter writer, FileWriterHandler handler);
}
class SwingWorkerFileWriterService
implements FileWriterService
{
void handleWriteRequest(FileWriteRequest request, FileWriter writer, FileWriterHandler handler) {
Worker worker = new Worker(request, fileWriter, fileWriterHandler);
worker.execute();
}
static class Worker extends SwingWorker<File,Void> {
// set in constructor
private FileWriter fileWriter;
private FileWriterHandler fileWriterHandler;
private FileWriterRequest fileWriterRequest;
protected File doInBackground() {
return fileWriter.writeFile(fileWriterRequest);
}
protected void done() {
fileWriterHandler.done();
try
{
File f = get();
fileWriterHandler.handleFileWritten(f);
}
catch (Exception ex)
{
// you could also specifically unwrap the ExecutorException here, since that
// is specific to the service implementation using SwingWorker/Executors.
fileWriterHandler.handleFileError(ex);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
系统的每个部分都是可单独测试的 - 应用程序逻辑,表示(成功和错误处理)以及线程实现也是一个单独的问题.
这可能看起来像很多接口,但实现主要是从原始代码中剪切和粘贴.接口提供了使这些类可测试所需的分离.
我不太喜欢SwingWorker,所以将它们放在界面后面有助于防止它们产生的混乱.它还允许您使用不同的实现来实现单独的UI /后台线程.例如,要使用Spin,您只需要提供FileWriterService的新实现.