27

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, param) because then it's not using Spring -- the @PostConstruct method is not getting fired.

Am I missing something obvious here?

public class Connection {
    private String x1;
    private String x2;

    public Connection(String x1, String x2) {
        this.x1 = x1;
        this.x2 = x2;
    }

    @PostConstruct
    public void init() {
        x1 = "arf arf arf";
    }

}


@Test
public void test() {
    Connection c = new Connection("dog", "ruff");
    assertEquals("arf arf arf", c.getX1());
}

I have something similar (though slightly more complex) than this and the @PostConstruct method does not get hit.

4 Answers 4

27

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.

7
  • That's what I ended up doing. May 9, 2012 at 11:51
  • 10
    the init can be private Nov 12, 2015 at 16:11
  • 1
    @shanyangqu Then you could use ReflectionTestUtils or equivalent for instance. It's test code afterall.
    – mrembisz
    Nov 12, 2015 at 16:19
  • @mrembisz I know, just thinking its worth mentioning... also I find deencapsulate is more powerful.... thean ReflectionTestUtils Nov 13, 2015 at 13:44
  • 1
    @AHungerArtist this should be added to the accepted answer. I did not want to burden my test with the entire SpringJUnit4Runner class and simply calling the method seemed to work just fine. This answer works in cases where the tester wants to test the target class without worrying about the spring context.
    – Kent Bull
    Dec 22, 2015 at 21:49
14

Have a look at Spring JUnit Runner.

You need to inject your class in your test class so that spring will construct your class and will also call post construct method. Refer the pet clinic example.

eg:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:your-test-context-xml.xml")
public class SpringJunitTests {

    @Autowired
    private Connection c;

    @Test
    public void tests() {
        assertEquals("arf arf arf", c.getX1();
    }

    // ...
4
  • I understand that, as I stated. But how do I test multiple permutations of this constructor inside one test class? What am I not making clear about my question? May 9, 2012 at 9:31
  • I want five test cases for this class's constructor. Is there really no better way than to have an app context with five such beans and load it up? May 9, 2012 at 9:35
  • In real time if there are 5 possible ways to inject your class then am afraid to say that you have to create 5 bean definitions in that case.
    – Sridhar G
    May 9, 2012 at 9:42
  • 2
    I'll just test the @PostConstruct method by calling it on the object. May 9, 2012 at 11:51
0

@PostConstruct must be changing the state of the object. So, in JUnit test case, after getting the bean check the state of the object. If it is same as the state set by @PostConstruct, then the test is success.

1
  • The problem isn't how to test that it's changed, but that @PostConstruct is not getting fired. I'll edit my post to make that more clear. May 9, 2012 at 9:24
-1

By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘‘ in bean configuration file.

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

or

<context:annotation-config />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.