jeudi 28 septembre 2017

Testing servlets with spring beans and embedded jetty

What is the best way to setup embedded jetty to use Spring test context for unit tests? We are having problems testing legacy servlets which were sprinkled up with Spring beans like this:

public class MyServlet extends HttpServlet {

  @Autowired
  private MachineDao machineDao;
  void init() {
     SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, this.getServletContext());
  }
  ...

We are using embedded jetty (v9.2) to start Server with a particular servlet to test in spring test.

@ClassRule
public static TestServer server = new TestServer(MyServlet.class);

Problem is that spring test context is not accessible in jetty so processInjectionBasedOnServletContext(...) fails.

We also want to test that .jsp is rendered correctly, so using just new MyServlet() and MockHttpServletRequest & MockHttpServletResponse approach is not really a viable option.

We are using Spring 4.11 (not MVC) with java config & annotations.
Any help much appreciated!

Aucun commentaire:

Enregistrer un commentaire