title: Test servlet API with Mockito
body: <p>I have <code>AddBookController</code>:</p>
  
  <pre><code>public class BookAddController extends HttpServlet {
  
      @EJB(name = "BuyerServiceEJB")
      private BuyerServiceInterface buyerService;
  
      @EJB(name = "AuthorServiceEJB")
      private AuthorServiceInterface authorService;
  
      @Override
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          Book newBook = new Book();
          newBook.setName(request.getParameter("name"));
          newBook.setAuthor(authorService.findAuthorById(Long.parseLong(request.getParameter("author_id"))));
          newBook.setBought(Boolean.parseBoolean(request.getParameter("sold")));
          newBook.setGenre(request.getParameter("genre"));
          newBook.setPrice(Integer.parseInt(request.getParameter("price")));
          buyerService.addNewBook(newBook);
          doGet(request, response);
      }
  </code></pre>
  
  <p>And I have testing class:</p>
  
  <pre><code>public class BookAddControllerTest {
  
  final static Logger logger = Logger.getLogger(UserServiceBean.class);
  private Book testBook = new Book();
  private Author testAuthor = new Author();
  
  @Before
  public void precondition(){
      logger.info("Add Book Controller Test");
      testBook.setName("Возера Радасці");
      testAuthor.setAuthor_id(1L);
      testBook.setAuthor(testAuthor);
      testBook.setBought(false);
      testBook.setGenre("postmodernism");
      testBook.setPrice(15);
  }
  
  @Test
  public void testDoPost() {
      logger.info("Test post request processing");
      BookAddController bookAddController = Mockito.mock(BookAddController.class);
      HttpServletRequest testRequest = Mockito.mock(HttpServletRequest.class);
      HttpServletResponse testResponse = Mockito.mock(HttpServletResponse.class);
      BuyerServiceInterface buyerServiceInterface = Mockito.mock(BuyerServiceInterface.class);
      AuthorServiceInterface authorServiceInterface = Mockito.mock(AuthorServiceInterface.class);
  
      when(testRequest.getParameter("name")).thenReturn("Возера Радасці");
      when(testRequest.getParameter("author_id")).thenReturn("1");
      when(testRequest.getParameter("sold")).thenReturn("false");
      when(testRequest.getParameter("genre")).thenReturn("postmodernism");
      when(testRequest.getParameter("price")).thenReturn("15");
  
      bookAddController.doPost(testRequest, testResponse);
  
      verify(buyerServiceInterface, times(1)).addNewBook(testBook);
      verify(authorServiceInterface, times(1)).findAuthorById(Long.parseLong("1"));
      verify(bookAddController, times(1)).doGet(testRequest, testResponse);
  
  }
  </code></pre>
  
  <p>I receive exception:</p>
  
  <pre><code>java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
  
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.Class.getDeclaredMethods0(Native Method)
      at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
      at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
      at java.lang.Class.getMethod0(Class.java:3018)
      at java.lang.Class.getMethod(Class.java:1784)
      at org.junit.internal.builders.SuiteMethodBuilder.hasSuiteMethod(SuiteMethodBuilder.java:18)
      at org.junit.internal.builders.SuiteMethodBuilder.runnerForClass(SuiteMethodBuilder.java:10)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
      at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
      at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:98)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>Do I use correct approach of testing <code>doPost</code> method?</li>
  <li>How to fix this exception?  </li>
  </ol>