2

Why am I getting this exception:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.web.servlet.HandlerAdapter] is defined: expected single bean but found 0:

   public class MyControllerIntegrationTest {
Logger logger = Logger.getLogger(MyControllerIntegrationTest.class);
@Autowired
private ApplicationContext applicationContext;

private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HandlerAdapter handlerAdapter;
private TestExceptionController controller;

@Before
public void setUp() {
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    handlerAdapter = applicationContext.getBean(HandlerAdapter.class);

    controller = new TestExceptionController();
}

I am trying to run the above code. My application context is loaded and getting the above exception message. Please guide, should I add some bean definition in bean definition file?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Irene
  • 379
  • 1
  • 5
  • 9
  • Isn't the error self-explanatory? You have no bean of type `HandlerAdapter` in your test's context. You should show us the whole test, including class annotations, plus the test context file. – skaffman Nov 19 '11 at 19:39

2 Answers2

0

You should probably use the ApplicationContextAware interface, or change your code to directly inject the HandlerAdapter (by moving the @Autowired annotation to that field) instead of getting it from the app context. (Unless it's a prototype, not a singleton, and you really need a new instance on every unit test.)

millimoose
  • 39,073
  • 9
  • 82
  • 134
0

I had the same issue when going from Spring 3.0.7 to Spring 3.1.

I solved it using this answer: How to unit test a Spring MVC annotated controller?

Community
  • 1
  • 1
Alig
  • 314
  • 4
  • 17