3

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;
    }
}
ProjectDefy
  • 101
  • 1
  • 9

1 Answers1

1

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.

jiaweizhang
  • 809
  • 1
  • 11
  • 28
  • I'm getting an error with that. I've went and added it to the main post. (Sorry if I'm doing this stackoverflow thing wrong). – ProjectDefy Nov 20 '15 at 01:25
  • Ah. Easy fix. It's expecting a `File` but you're giving it a `String`. So just do what we did yesterday with `File f = new File("/ExplorerViewTestFolder")`. So that line should be: `explorer.countFilesAndFolders(new File("/ExplorerViewTestFolder"));` – jiaweizhang Nov 20 '15 at 01:28
  • I've tried that, unless I'm reading you wrong haha. Here, I did an update number 3 and it's giving me something else. – ProjectDefy Nov 20 '15 at 01:38
  • Ah. `static` - click the second option to make the method non-static (within the IDE). Read [this](http://stackoverflow.com/questions/3903537/i-want-to-know-the-difference-between-static-method-and-non-static-method) to learn more about static vs non-static – jiaweizhang Nov 20 '15 at 01:40
  • Ah gotcha, I see now. And I guess that's working fine now, but I'm getting a NullPointerException with my code now I guess. – ProjectDefy Nov 20 '15 at 01:44
  • Can you go ahead and post your complete `Explorer` code? The `NullPointerException` seems to be occurring somewhere within `explorer`, such as when `null` is returned because no files are present in a directory (yesterday's code) – jiaweizhang Nov 20 '15 at 01:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95636/discussion-between-jiaweizhang-and-projectdefy). – jiaweizhang Nov 20 '15 at 01:48