I am very new to Java Spring and I tried to set up 2 classes and a Test for one of them. That is my project structure:
Here is the code for the classes:
@Component
public class Box {
private Item item;
@Autowired
public Box(Item item) {
this.item = item;
}
public Item getItem() {
return item;
}
}
@Configuration
@ComponentScan
public class BoxConfig {}
@Component
public class Item {}
And here is the code of the JUnit Test I wrote in the BoxTest class:
@ContextConfiguration(classes = {BoxConfig.class})
public class BoxTest {
@Autowired
Box box;
@Test
public void BoxTest1() {
Assertions.assertNotNull(box.getItem());
}
}
My Expectation was, that Spring automatically injects a box object into the box attribute of the Test, but the box attribute ist always null so I keep getting a NullReferenceException. Does someone know why it is not properly initialised?