-5

Edited I am learning how to write testcases using JUnit. Disclaimer: I haven't understood anything that I have found on the net regarding this. :) I wish to write a simple test case for this: I have a simple Java class that accepts arguments as parameters to the main function and prints it. There is no more to this code. Edit:

This is my main function: public static void main(String args[]) {

    //Expecting 3 arguments: 1. InputDirectory path, 2. OutputDirectory path, 3. Keys-comma separated

    checkArguments(args);

.....

private static void checkArguments(String[] args) {
    // TODO Auto-generated method stub
    if (args.length != 3) {
        try {
            System.out.println("Invalid input arguments");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return;
    }
}

}

This main function code works well. Now I need to write a test case such that I check how many inputs are there and accordingly print the message. So far, I have written this:

FilterProcessPDK checkArg = new FilterProcessPDK();
int testCheckArgRes = checkArg.checkArguments(); //I dont know how to pass the arguments to this function

@Test
public void testCheckArgs(){
    assertEquals("message", expected, actual);
}

For this, I will be giving the inputs- 1) Input path (C:/xyz/input) 2) Output path (C:/xyz/output) 3) A list of any number of "keys" ("A" or "A, j")

Shivika P
  • 115
  • 8
  • What did you try or think of by yourself? – YoungHobbit Jan 11 '16 at 10:29
  • So I created an object of the "ArgsInput1" class in the test case class and tried to pass the parameters by calling the main function using that object. Naturally, it threw an error and I don't know what else to try with it. – Shivika P Jan 11 '16 at 10:32
  • "Naturally, it threw an error" Why's that "naturally"? Sounds like it'd work - you can call a main method like any other. – Andy Turner Jan 11 '16 at 10:51
  • There's only one kind of argument: an array of Strings. Sounds like an easy, unnecessary test. – duffymo Jan 11 '16 at 12:38
  • Andy Turner, "naturally" because it was expected. I am new to this and I was expecting it to throw an error :) – Shivika P Jan 12 '16 at 03:05
  • duffymo, it is unnecessary (according to me) but the team wants it to be done- thus the task. – Shivika P Jan 12 '16 at 03:05
  • In your comment to @duffymo's answer you write that the code has already been written. Could you show us the real code then, please? Your example code doesn't make much sense as it is right now to me. (BTW, I think your team played a trick on you ;) ) – Ivo Mori Jan 12 '16 at 20:28
  • @IvoMori, haha no that's for the quality assessment. I would show the code but it is around 1000 lines. :) The snippet that I have put here is directly from the code itself. – Shivika P Jan 13 '16 at 05:36

1 Answers1

2

I've been writing tests with Junit since I first became aware of it in 1998, but I've never seen such a test.

I don't think it's appropriate.

I have to admit that I don't usually test main methods. It ought to be orchestrating a bunch of other stuff that I've tested well. By the time I get to the main method I've got sufficient coverage and confidence where I don't test input and output issues.

A better solution is programming by contract: Have your main method enforce the pre-conditions for inputs that it requires. If users provide incorrect inputs, let them know.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Hi duffymo, the thing is, the code has already been written. I have been asked to write the unit test cases for quality purpose. Now, I don't really know how to write the test case for that. I don't really find the point in writing it after the code has been written and executed successfully. Any suggestions on workarounds? – Shivika P Jan 12 '16 at 03:04
  • None from me. It feels like a wild goose chase. – duffymo Jan 12 '16 at 22:51