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));
}
}
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:
With this approach, you can test the importing logic without even thinking about the GUI part.