2

I've seen the link Spring Test session scope bean using Junit that shows how to setup Junit to test @Session scoped beans, but how do I set up a Junit test case to test a Spring bean that has a @Session scoped bean @Autowired into it.

Community
  • 1
  • 1
user497087
  • 1,561
  • 3
  • 24
  • 41

1 Answers1

1

If you're testing the behaviour of the spring bean, the easiest way to do this is to mock the object and inject it yourself using ReflectionTestUtils:

class SpringBean {
    @Autowired Other other;

    public void method() {
        // ...
    }
}

class SpringBeanTest {
   @Test public void testIt() {
       Other other = new Other();
       SpringBean bean = new SpringBean();
       ReflectionTestUtils.setField(bean, "other", other);
       // test it
   }
}
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171