0

I am trying to write a test that loads different properties files from String array. But the code keeps throwing a null pointer exception, any ideas please?

@RunWith(value = Parameterized.class)
public class AllTests 
{ 
     private static String text;
     private static Properties props;

     public AllTests(String text) 
     {
        AllTests.text= text;
     }

     @Parameters
     public static List<String[]> data() 
     {
       String[][] data = new String[][] { { "test.properties" }};
    return Arrays.asList(data);
     }

         @BeforeClass
     public static void setup()
     {  
           props = new Properties();
         try 
         {
            //load a properties file 
        props.load(new FileInputStream(text));
         } 
         catch (IOException ex) 
         {
        ex.printStackTrace();
         }
 }

 @Test
 public void test() 
 {
        System.out.println(text);
 }}

I did some further investigation and discovered the @Test stub works but the @BeforeClass returns null, can I not use the parameters in the setup?

@RunWith(value = Parameterized.class)
public class AllTests 
{ 
     private static String client;

public AllTests(String client) { AllTests.client = client; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { "oxfam.properties" }}; return Arrays.asList(data); } @BeforeClass public static void setup() { System.out.println(client); } @Test public void test() { System.out.println(client); }}

1
  • props.load(new FileInputStream(client)); this the line that throws the null pointer Commented May 22, 2013 at 15:41

2 Answers 2

3

The props class variable is never initialized. Try initializing it on declaration:

private static Properties props = new Properties();
Sign up to request clarification or add additional context in comments.

1 Comment

@DavidCunningham Then text is null.
1

As Brent says, the original error was because props wasn't initialised. However, the reason your test doesn't work is because you're using static fields. They should be instance fields, only the data() should be static.

The following works:

@RunWith(value = Parameterized.class)
public class AllTests {
  private String text;
  private Properties props;

  public AllTests(String text) {
    this.text = text;
    props = new Properties();
    try {
      // load a properties file
      props.load(new FileInputStream(text));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  @Parameters
  public static List<String[]> data() {
    String[][] data = new String[][] { { "test.properties" } };
    return Arrays.asList(data);
  }

  @Test
  public void test() {
    System.out.println(text);
  }
}

JUnit calls the data() method and then creates an instance of the AllTests class for each value returned by the data() method, with the constructor parameters which also come from the data() method. Therefore your text and props fields should be instance fields.

So in your example, your @BeforeClass was called before the constructor, hence the null pointer exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.