If I have a class with a @PostConstruct method, how can I test its constructor and thus its @PostConstruct method using JUnit and Spring? I can\'t simply use new ClassName(param
If the only container managed part of Connection
is your @PostContruct
method, just call it manually in a test method:
@Test
public void test() {
Connection c = new Connection("dog", "ruff");
c.init();
assertEquals("arf arf arf", c.getX1());
}
If there is more than that, like dependencies and so on you can still either inject them manually or - as Sridhar stated - use spring test framework.