1

I'm using JUnit for unit testing. Let's say I want to test class B (methods of class B). Let's say we have another class A which is the main class (contains main method) and has some protected static fields.

Now, it is the case that class B uses some of these static fields of class A. So if I'm testing class B these static fields of class A does not exist.

How can I test class B without executing the program (executing class A)?

Edit: I have to clarify it. Let's assume we have the following class A in src/package1/classA.java:

public classA {
   protected static int field1;
   protected static int field2;

   public static void main(String[] args) {
      // initialize static fields.
   }
}

Now lets assume we have another class B in the same package src/package1/classB.java.

public ClassB {
       public ClassB() {
            // Do some stuff.
       }

       public void someMethod() {
           // Access of static fields from A.
           classA.field1....
           classA.field2....
       }          
}

Now I have a JUnit test in test/package1/classBTest.java for testing class B. But the problem is that field1 and field2 are not initialized.

How can I manually initialize in JUnit the two fields classA.field1 and classA.field2 without executing the main method of class A?

6
  • Do you mean private static fields when you talk about class A? If they are private then class B will not be permitted to access them, whether using JUnit or otherwise. Commented Oct 5, 2014 at 10:15
  • No no I mean protected static fields in class A. class A and class B are in the same package. Commented Oct 5, 2014 at 10:18
  • 1
    I think you'll have to show us the code which throws the error, and the exact error message which is thrown. It sounds like you should have no problem referring to static fields in A from B so you need to edit your question and provide more information. Commented Oct 5, 2014 at 10:23
  • Also, generally, this is an indication of a design problem. Commented Oct 5, 2014 at 10:24
  • 1
    In your test, initialize A.field1 and A.field2, and then call bUnderTest.someMethod(). But I agree this is ugly design. Why arent field1 and field2 passed as argument to the method, or to the B constructor? Global, mutable state is something you want to avoid at all cost. Commented Oct 5, 2014 at 12:15

1 Answer 1

1

You could call the main method of classA .i.e. ClassA.main(somestrArray) and it should do the initialization.

But if you don't want to do that then you could create your junit test in the same package as the original class and you would be able to access the protected variables .i.e. ClassA.field1 =1; etc. Btw it does not have to be in the same project, just the package names should be the same.

If thats not OK, then you would need to refactor your ClassA to allow for this scenario .i.e. have a method that does the init etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Is it better practice to use static fields in class A and use these fields in class B like ClassA.field1 or is it better to use non static fields in class A and then pass these fields as arguments to a function in class B (e.g. the constructor)? I'm just meaning from the programming point of view, not in relation to JUnit.
I would personally go with non static variables

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.