2
votes

I'm trying to autowire a bean in the MVC Controller class, but I can not get another value, than null. When I put throw new Error("E") in constructor of the bean to be injected into the helloWorldController bean, I get an Exception: Error creating bean with name helloWorldController: Injection of autowired dependencies failed. But when I run test without error in constructor, I don't get the bean, I get null.

I'm totally confused. What does it do? It is trying to create and inject a dependency, when it creates the controller instance. Ok, so why the variable is not inicialized, if error did not occured?

I have extended my post at the bidding of Sean Patrick Floyd:

package testy.sprung;

//import declarations ommited

import testy.sprung.beany.AwiredBean;

@Controller
public class HelloWorldController {
    private Logger log = Logger.getLogger("springTestLogger");
    @Autowired
    private AwiredBean oz;

    @RequestMapping("/sprung")
    public ModelAndView base() {
        log.debug("base URI");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("firstPage");
        return mv;
    }

    @RequestMapping(value="/{articel}/{subTitle}",method=RequestMethod.GET)
    public ModelAndView szia(@PathVariable("articel") String articel, @PathVariable("subTitle") String st, @RequestParam(value="co", required=false) String co) {
        log.debug("Path GET/{articel}/{subtitle}: " + articel + "/" + st + "?co=" + co);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index"); // now put index.jsp in /WEB-INF/files
        mv.addObject("articel", articel);
        mv.addObject("subtl", st);
        mv.addObject("co", co);
        mv.addObject("awir", oz); //but it is null
        return mv;
    }
}

The bean implements any empty interface:

package testy.sprung.beany;

public class AwiredBeanImpl implements AwiredBean {
    @Override
    public String toString() {
        return "CommonAutowired";
    }

    public AwiredBeanImpl() {
        throw new Error("E");
    }
}

I run it inside the test. The test fails, because wether my error or NullPointerException is thrown:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:WebContent/WEB-INF/sprung-servlet.xml")
public class ZakladniExtendedTest extends TestCase {
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private HelloWorldController controller;
    @Inject
    private ApplicationContext context;

    public ZakladniExtendedTest() {
        PropertyConfigurator.configure("t-resources/log4j.properties");
    }

    @Before //this method is called before each test
    public void setUp() {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        controller = new HelloWorldController();
    }

    @Test
    public void testThemeResolverExists() { //this test works
        assertTrue(context.containsBean("themeResolver"));
    }

    @Test
    public void autowiringTest() throws Exception { //but this not
        request.setRequestURI("/title/subtitle");
        request.setMethod("GET");
        request.setParameter("co", "param");
        ModelAndView mav = new AnnotationMethodHandlerAdapter().handle(request, response, controller);
        String viewName = mav.getViewName();
        Map objects = mav.getModel();

        assertEquals("index", viewName);
        //NullPointerException follows:
        assertEquals("CommonAutowired", objects.get("awir").toString());
    }
}
1
please show the controller class and the relevant bit of spring configSean Patrick Floyd

1 Answers

8
votes

Instead of controller = new HelloWorldController(); you should inject your controller so that it becomes a Spring-managed bean.