2

I´ve using JUnit but some tests have some issues, these tests have @Autowired annotation inside the Spring beans and when i reference them the beans that are @Autowired were always NULL.

Here is the example code:

     public class Test {

                 protected ApplicationContext ac;

                 @Before
                 public void setUp() {
                     ac = new FileSystemXmlApplicationContext("classpath:config/applicationContext.xml"
                     "classpath:config/test-datasources.xml");
                 }

                 @Test
                 public void testRun() throws Exception {
                        IManager manager =  (IManager)this.ac.getBean("manager");
                        manager.doSomething();
                 }
    }

    @Service
    public class Manager implements IManager {

            public boolean doSomething() throws Exception {
                 ParametersJCSCache parametersJCSCache = new ParametersJCSCache();
                 String paramValue = parametersJCSCache.getParameter("XPTO");
                 ...
            }
    }

    public class ParametersJCSCache extends SpringBeanAutowiringSupport {

          @Autowired
          private IParameterManager parameterManager;  //THIS IS NULL
    }

When invoking the Manager object the Spring generates the proxy but when accessing the @Autowired parameterManager the object is null, and with this issue i can´t test this method.

Any idea what is causing this? Why does the object not get injected? It works well in the context of a web application, but in the test context is always NULL.

abhi
  • 1,760
  • 1
  • 24
  • 40
bigster
  • 63
  • 1
  • 1
  • 9

3 Answers3

3

Try running your unit tests with a spring runner instead.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/applicationContext.xml",
                 "classpath:config/test-datasources.xml"})
public class MyTest {

   @Autowired private IManager manager;

   @Test public void someTest() {
     assertNotNull(manager);
   }
}

It will load the config only once and when needed, and it will autowire your junit class

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
2

I've finally found a solution to my problem. Find this post SpringBeanAutowiringSupport does not inject beans in jUnit tests and do something like this the JUnit tests works.

Community
  • 1
  • 1
bigster
  • 63
  • 1
  • 1
  • 9
0

Looks to me like ParametersJCSCache is not a Spring managed bean. You need to make this a Spring bean and autowire it into the Manager

@Service
        public class Manager implements IManager {

                public boolean doSomething() throws Exception {
                     ParametersJCSCache parametersJCSCache = new ParametersJCSCache(); <-- You create a new (non Spring-managed) instance
                     String paramValue = parametersJCSCache.getParameter("XPTO");
                     ...
                }
        }

Change to:

@Service
public class Manager implements IManager {

      @Autowired
      private ParametersJCSCache  parametersJCSCache; 

        public boolean doSomething() throws Exception {
             String paramValue = parametersJCSCache.getParameter("XPTO");
             ...
        }
}

@Component
public class ParametersJCSCache extends JCSCache {

      @Autowired
      private IParameterManager parameterManager;  //THIS IS NULL
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • Correct. That why i´m using Autowired, if ParametersCache was a spring managed bean i don´t need to have Autowired the Manager bean. This works in the Web application context. – bigster Oct 20 '14 at 10:16
  • If ParametersJCSCache is a non-Spring managed bean how on earth can IParameterManager be injected by Spring? – Alan Hay Oct 20 '14 at 10:18
  • That´s the mystery, this works when invoking the same code when i´m inside a web context. Using JUnit don´t get the same behaviour. And the ParametersJCSCache class extends the SpringBeanAutowiringSupport. Forget to put this in the code. – bigster Oct 20 '14 at 10:28
  • Well it' not working in the way you think it's working. – Alan Hay Oct 20 '14 at 10:30
  • @Alan : cant we use the existing autowired configurations in test? Any way to do it ? – Raghuveer Jul 17 '18 at 07:47