Reputation: 191
My test already extends an abstract BaseIntegrationTest class, so can't extends AbstractTestNGSpringContextTests
class - as we normally do with Testng Spring integration tests, and I don't want my BaseIntegrationTest to load the spring application context (by extends AbstractTestNGSpringContextTests
) because there are a lot more sub-class tests that don't need the context (which has a DB entity), and most of the time I will exclude current test that requires the DB connection for integration test. So once in a while I need to run this Testng based test, which extends some class already and still needs to contextAware (I have tried implements ApplicationContextAware
which doesn't work for me, maybe I was doing it it wrong?). How would you do it?
Upvotes: 1
Views: 1424
Reputation: 15316
That's just how Spring integrates with TestNG - you have to extend AbstractTestNGSpringContextTests
. Alternatively you can manually instantiate Spring in your tests:
new ClassPathXmlApplicationContext("context1.xml", "context2.xml")
But that won't allow you to use other kind of annotations like @Transactional
. Potentially you could write your own integration with Spring over TestNG listeners (hm.. why Spring didn't use it instead?), but that's going to take some time to implement and debug :)
Also, I'd like to mention that extending your own base classes is not a good idea. Though it's very common unfortunately. A much better (easier to read, OOP-friendlier) way would be to create separate classes and functions with your common logic. And then invoke those explicitly in your tests.
Upvotes: 2