1

I am using spring with dependency injection and came across this puzzling piece of code in my TestNG class and want to clear my head around this issue

I have this below code

public class myBase {

    @Autowired @Lazy @Qualifier("someInstanceA")
    protected SomeClass someInstanceA;
.
.
}

public class myTestB extends myBase {
    private String varB = someInstanceA.getVarB();

    @Test
    .
    .
}

This above code gave me a NullPointerException at line

 private String varB = someInstanceA.getVarB();

But when I do this below

 public class myTestB extends myBase {
    private String varB;

    @BeforeClass
    private void getVarB() {
        varB = someInstanceA.getVarB();
    }

    @Test
    .
    .
}

The tests ran fine. I read that BeforeClass is like a default constructor and eager initialization is similar to initializing a variable using a default constructor. What am I missing here?

Cœur
  • 37,241
  • 25
  • 195
  • 267
yalkris
  • 2,596
  • 5
  • 31
  • 51
  • 1
    http://stackoverflow.com/questions/6335975/autowired-bean-is-null-when-referenced-in-the-constructor-of-another-bean – Savior Apr 22 '16 at 16:38
  • http://stackoverflow.com/questions/15627792/order-in-which-fields-in-a-bean-are-initialized – Savior Apr 22 '16 at 16:38

1 Answers1

2

Short answer : Bean injection happens after creation of instances of your class.

Since in the creation of myTestB (which should start with an uppercase, by the way), you call getVarB on someInstanceA which is not yet injected, you get a NullPointerException.

The second case works because @BeforeClass run one time before first test run, after object creation. Thus when calling someInstanceA#getVarB in getVarB method, someInstanceA is already autowired and not null.

Savior
  • 3,225
  • 4
  • 24
  • 48
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76