Search code examples
javajunit4

value of global variable is changing to null in JUnit


I wrote a simple class, in which first function's output is second function's input and second function's output is third function's input. After that I am printing these outputs in another class which implements JUnit @Test cases.

But for call to second function's test, I'm getting 'res' value as null(which should be output of first function and passing it as input to second function). Same is the case with third function.

I'm declaring 'res' as global variable. Then why is its value changing to null instead of holding the result of first function and then of second function( for call to third function) ?

Here is the class which contains 3 functions:

package con.nc.junitexmples.Junit4Examples;

public class StringExample {

 public static String firstFunction() {

    String msg1 = "msg1";
    return msg1;

}

public static String secondFunction(String msg1) {

    String msg2 = msg1+"msg2";
    return msg2;

}

public static String thirdFunction(String msg2) {

    String msg3= msg2+"msg3";
    return msg3;

  }

}

Here is class which implements JUnit @Test cases and printing output:

package con.nc.junitexmples.Junit4Examples;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class TestStringExample {

String res;

  @BeforeClass  
    public static void setUpBeforeClass() throws Exception {  
      System.out.println("\n\t\t-----JUnit cascading input Example------\n\n");  
      System.out.println("before class---->only once");  


    }  
    @Before  
    public void setUp() throws Exception {  
        System.out.println("\nbefore--->before each test case");  
    }  

    @Test  
    public void testFirstFunction(){  
        System.out.println("\ttest case: FIRST Function");  

        res = StringExample.firstFunction();
        System.out.println("\t"+res);

    }  

    @Test  
    public void testSecondFunction(){  
        System.out.println("\ttest case: SECOND Function");  

        res = StringExample.secondFunction(res);
        System.out.println("\t"+res);

    }  

    @Test  
    public void testThirdFunction(){  
        System.out.println("\ttest case: THIRD Function");  

        res = StringExample.thirdFunction(res);
        System.out.println("\t"+res);

    }  


    @After  
    public void tearDown() throws Exception {  
        System.out.println("after ---> after each test case\n\n");  
    }  

    @AfterClass  
    public static void tearDownAfterClass() throws Exception {  
        System.out.println("after class--->only once");  
    }  

}

And here is the output I'm getting:

    -----JUnit cascading input Example------


 before class---->only once

 before--->before each test case
     test case: FIRST Function
     msg1
 after ---> after each test case



 before--->before each test case
     test case: SECOND Function
    **nullmsg2** 
 after ---> after each test case



 before--->before each test case
    test case: THIRD Function
     **nullmsg3**
 after ---> after each test case


after class--->only once

How do I pass result of first function to second as input and so on?


Solution

  • Each of these tests is self contained, independent and the value of res at the start of these tests methods is null.

    So, that explains why you are seeing:

    nullmsg2
    nullmsg3
    

    Given this question from the comments above:

    how do I pass output of first function as input to second function for second test?

    You could test all three calls in one test method, like so:

    @Test
    public void testAll() {
        String res = StringExample.firstFunction();
        Assert.assertEquals("msg1", res);
    
        res = StringExample.secondFunction(res);
        Assert.assertEquals("msg1msg2", res);
    
        res = StringExample.thirdFunction(res);
        Assert.assertEquals("msg1msg2msg3", res);
    }