mercredi 7 septembre 2016

How can I use JUnit to test a class that is passed a parameter from another class? [duplicate]

This question already has an answer here:

I'm new to both Java and unit testing. Maybe I haven't been able to find the right search terms here, but if someone can point me in the right direction, I'd really appreciate it.

Basically, here's my class, main.java

public class Main {

    public static void main(String[] args)  {

        System.out.println("Methods will be inflicted on these files!");

        //calls method on files
        newMethod method1 = new newMethod("example1.txt");
        newMethod method2 = new newMethod("example2.txt");
    }
}

Okay, cool. Here is newMethod.java. The purpose of newMethod.java is to take a file parameter and perform some tasks on it.

public class newMethod {

     newMethod(String file) {

         //the code that follows takes the contents of the file and transforms it in some way. 
     }
}

Now I'm developing some tests using JUnit. I can easily write test cases for Main.java by just feeding the method sample files to read, which are then passed to newMethod.java. Here's an example of MainTest.java.

public class MainTest {
    @Test
    public void testMain() {

        newMethod testMethod = new newMethod("test.txt");

        assertEquals( //more code )
    }
}

I want to run some tests directly on newMethod.java. But the problem is, newMethod.java relies on Main.java to pass it a parameter (String file). How can I write a test that passes a parameter to newMethod.java without relying on Main.java? Or can I only run tests on Main.java since that's what provides the parameter?

Aucun commentaire:

Enregistrer un commentaire