user1462
user1462

Reputation: 21

Autowired not working with TestNG Test Class

I am unable to access an autowired class (ListService) located in a test class. Outside of the test, I can successfully access the autowired class to make the following call:

List schoolSubjectList = listService.getMathSubjectList();

The above call results in a null pointer exception.

Test Class:

...
@Autowired
ListService listService;

@BeforeClass
public void setUp(ITestContext context) throws Exception {
    Random random = new Random();
    int randomInteger = Math.abs(random.nextInt(1000));

    suiteParams = context.getSuite().getXmlSuite().getAllParameters();

    List<SchoolSubjectDto> schoolSubjectList = listService.getMathSubjectList();
    int sizeOfSchoolSubjectList = schoolSubjectList.size();

    ...

Upvotes: 2

Views: 3332

Answers (1)

Alexandre FILLATRE
Alexandre FILLATRE

Reputation: 1327

Make sure your class implements AbstractTransactionalTestNGSpringContextTests.

For a full example, see mkyong.com/unittest/testng-spring-integration-example

Upvotes: 5

Related Questions