如何为仅在程序运行时被调用的函数编写 JUnit 测试?

huangapple go评论71阅读模式
英文:

How to write JUnit tests for a function that is only called when the program is run?

问题

以下是您提供的内容的中文翻译:

我有一个Java程序,当运行时,会显示一个带有导入文件按钮的GUI。我想为导入文件方法编写一个单元测试,以确保该方法能够完整执行,但是该方法仅在按下按钮时调用,而按钮只能在手动运行程序时才能使用。对于这种情况,最佳的方法是什么?

要测试的方法:

public class FileClass {
    public Boolean import(someVar1, someVar2, someVar3) {
        Boolean success = false;
        // ......
        // 点击一些按钮,选择文件,然后点击确认
        // ......
        return success;
    }
}

我的JUnit测试:

public class FileClassTest {
     @Test
     public void importTest() {
        // ....
        // ....
        assertTrue(FileClass.import(x, y, z));
     }
}

请注意,代码部分已被保留为原文,未进行翻译。

英文:

I have a Java program that when run, displays a GUI with a button to import a file. I want to write a unit test for the import file method to make sure the method executes all the way through, but that method is only called when the button is pressed which is by extension only available when the program is run manually. What's the best approach for something like this?

Method to test:

public FileClass{
    public Boolean import(someVar1, someVar2, someVar3){
        Boolean success = false;
        ......
        click some buttons, choose the file, and click OK
        ......
        return success;
    }
}

My junit test:

public class FileClassTest{
     @Test
     public void importTest(){
        ....
        ....
        assertTrue(FileClass.import(x,y,z));
     }
}

答案1

得分: 2

我会尝试使用 AssertJ 框架进行 Swing GUI 测试。他们在 一个带有示例项目的代码仓库 中。

assertj-swing-junit-examples 项目应该是一个不错的起点。

英文:

I would try AssertJ framework for Swing GUI testing. They have a repository with the sample projects.

assertj-swing-junit-examples project should be a good start.

答案2

得分: 2

如果您想针对导入逻辑运行测试 - 这与GUI无关。

所以这完全取决于您的代码 - 并非每段代码都适合单元测试,因此您可能需要重构所需的功能。

考虑以下对您所提出内容的“逻辑”重构:

public class MyGui {
    private DataImporter dataImporter;
    public MyGui(DataImporter dataImporter) {
      this.dataImporter = dataImporter;
    }
    public Boolean import(a, b, c) {
       // 所有的UI操作在这里,
       // 然后在您收集到所有数据后:
       byte [] dataToImport = .... 
       return dataImporter.importData(dataToImport, a, b, c);
      
    } 
}

interface DataImporter {
    /*
     * 根据配置参数a、b、c,将识别的数据流导入字节数组数据(再次强调,这是一个示意性的例子)。
     * 封装了基于不同参数的数据导入逻辑
     */
    boolean importData(byte [] data, a, b, c);
}

通过这种方法,您可以在不考虑GUI部分的情况下测试导入逻辑。

英文:

If you want to run a test for the logic of the import itself - it should have nothing to do with GUI at all.

So it all depends on your code - not every code is unit-testable, so you might need to refactor the needed functionality.

Consider the following "logical" refactoring to what you've presented:

public class MyGui {
    private DataImporter dataImporter;
    public MyGui(DataImporter dataImporter) {
      this.dataImporter = dataImporter;
    }
    public Boolean import(a, b, c) {
       // all UI operations here, 
       // and then when you've gathered all the data:
       byte [] dataToImport = .... 
       return dataImporter.importData(dataToImport, a,b,c);
      
    } 
}

interface DataImporter {
    /*
     * Depending on the configuration parameters a,b,c will import a stream of data identified by byte [] data (again its a schematic example). 
     * Encapsulates logic of data importing based on different parameters
     */
    boolean importData(byte [] data, a, b, c);
}

With this approach, you can test the importing logic without even thinking about the GUI part.

huangapple
  • 本文由 发表于 2020年7月25日 04:16:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63080865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定