10879

JUnit initialization of static fields

Question:

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?

Answer1:

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.

Recommend

  • Google protocol buffers - user defined java objects as messages fields
  • Not able to get sonarqube to index files in my multi module gradle project
  • Inheritance in std::map with base class as value
  • objective C: use NSMutableArray in different classes
  • Jaxb - Overriding the XMLElement name attribute
  • how do i get rid of duplicate clojure test-is unit tests on the REPL
  • C++ Pointer to Member Function as Template Default Argument
  • How to make an elasticsearch query that filters on the maximum value of a field?
  • ValidationResult Returned From IValidatableObject.Validate Is Not Localized
  • Javascript inside script tag in an Angularjs template is not executed
  • What is the JSF behaviour, if you bind the same backing bean property to two input fields in the sam
  • Extract decision boundary with scikit-learn linear SVM
  • UIImagePickerControllerDelegate Methods Not Called When Delegate Not UIViewController
  • Programatically open file in visual studio
  • distinct values from multiple fields within one table ORACLE SQL
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • jQuery: add elements until a particular height is reached
  • Memory error in python- how to use more memory
  • Combining two different ActiveRecord collections into one
  • Differences in dis-assembled C code of GCC and Borland?
  • How to view images from protected folder with php?
  • Display images in Django
  • How to use carriage return with multiple line?
  • Algorithm for a smudge tool?
  • Fetching methods from BroadcastReceiver to update UI
  • Resize panoramic image to fixed size
  • How to set my toolbar fixed while scrolling android
  • Symfony2: How to get request parameter
  • Importing jscolor library in angular 2
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • WPF Applying a trigger on binding failure
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?