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

Stack Overflow Asked by thePandasFriend on January 3, 2022

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{
     public void importTest(){

2 Answers

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.

Answered by Mark Bramnik on January 3, 2022

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.

Answered by CrazyCoder on January 3, 2022

Add your own answers!

Related Questions

C# find the greatest common divisor

11  Asked on January 5, 2022 by user2723261


Time series summary

2  Asked on January 5, 2022 by naanan_


Compute frequencies for groups of variables

3  Asked on January 5, 2022 by armel-tedjou


How to plot entropart Diversity profiles in ggplot2

1  Asked on January 5, 2022 by alejandra-dip


Laravel 6.4.1 SQLSTATE[HY000] [2002] Connection refused

12  Asked on January 5, 2022 by manish-mahajan


How to get the key of an element in firebase with AngularFire2?

1  Asked on January 5, 2022 by juan-esteban-rios-gonzalez


(PowerShell) How do I filter usernames with Get-EventLog

3  Asked on January 5, 2022 by jeremiah-williams


pandas isin() fail between dataframes with objects types columns

1  Asked on January 5, 2022 by franco-milanese


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir