ProjectDefy
ProjectDefy

Reputation: 101

JUnit Test Case need to test a directory

I'm having difficult time figuring out how to test a directory that I've made myself and imported in the Project Files.

Currently I have:

File file = new File(ExplorerView.class.getResource("/ExplorerViewTestFolder"));

Which I'm trying grab from here to testPicture

ExplorerView explorer = new ExplorerView();

explorer.countFilesAndFolders("/ExplorerViewTestFolder");

Edit2: Changed to

public class ExplorerViewTests {

@Test
public void testCountFilesAndFolders() {

    ExplorerView explorer = new ExplorerView();

    explorer.countFilesAndFolders("/ExplorerViewTestFolder");


}

Error: enter image description here

EDIT3: enter image description here

EDIT4:

public int countFilesAndFolders(File f) {
    if (f.isFile()) {
        return 1;
    } else {
        int total = 0;

        for (File file : f.listFiles()) {
            if (file.isHidden() == false) {
                total += countFilesAndFolders(file);
            }
        }
        return total + 1;
    }
}

Upvotes: 3

Views: 933

Answers (1)

jiaweizhang
jiaweizhang

Reputation: 819

A simple JUnit test looks something like this:

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class MyTests {

    @Test
    public void directoryTest() {

        // ExplorerView class is tested
        ExplorerView explorer = new ExplorerView();

        explorer.countFilesAndFolders("ExplorerViewTestFolder");

        // assert statements
        assertEquals("filecount must be same ", 12, explorer.getNumberFiles());
        assertEquals("directorycount must be same ", 2, explorer.getNumberDirectories());
    }

} 

If the assertEquals evaluates to true - if explorer.getNumberFiles() == 12 and explorer.getNumberDirectories() == 2, then the JUnit tests will pass. Otherwise, they will fail.

Upvotes: 1

Related Questions