title: How to test a method invocation inside an anonymous class using mockito
body: <pre><code>@Transactional(propagation = Propagation.REQUIRED)
      public void exe() {
          try {
              Reserve reserveInfo = newTransactionTemplate.execute(new TransactionCallback&lt;Reserve&gt;() {
                  @Override
                  public Reserve doInTransaction(TransactionStatus status) {
                      return reserveService.callReserve(reserveDetails);
                  }
              });
              if(reserveInfo != null &amp;&amp; reserveInfo.getStatus()=="DONE") {
              someOtherService.doSomething();
              }
          } finally {
              if(reserveInfo!= null &amp;&amp; reserveInfo.getStatus().equals("DONE")){
                  final String id = reserveInfo.getId();                                  
                  Release releaseInfo = newTransactionTemplate.execute(new TransactionCallback&lt;Release&gt;() {
                      @Override
                      public Release doInTransaction(TransactionStatus status) {
                          return reserveService.callRelease(id);
                      }
                  });             
                  if (releaseInfo.getStatus() != "RELEASED") {
                      throw new RuntimeException("Problem in releaseing");
  
                  }
              }
          }       
      }
  </code></pre>
  
  <p>This is the sample code I want to test. I couldn't find much on testing anonymous class on net. 
  I want to test the exe() method, go through newTransactionTemplate.execute method and mock the call to reserveService.callReserve(reserveDetails).</p>
  
  <p>Can any body please suggest how can I test this situation?</p>
  
  <p>here is the sample how i'm trying to test:</p>
  
  <pre><code> private SomeService someService = null;
      @Mock
      private ReserveService mockReserveService;
      @Mock
      private ReleaseService mockReleaseService;
      @Mock
      private TransactionTemplate mockTransactionTemplate;
      @Mock
      private SomeOtherService mockSomeOtherService;    
      @BeforeMethod
      public void setup() throws Exception {
         MockitoAnnotations.initMocks(this);   
         someService = new SomeService();
         someService.setReserveService(mockReserveService);
         someService.setReleaseService(mockReleaseService);
         someService.setSomeOtherService(mockSomeOtherService);
         someService.setNewTransactionTemplate(mockTransactionTemplate);
      }
      @Test(enabled=true)
      public void exeHappyPath() {
          Reserve reserveInfo = new Reserve();
          reserveInfo.setId("123");
          reserveInfo.setStatus("DONE");
          Release releaseInfo = new Release();
          releaseInfo.setStatus("RELEASED");
          when(mockReserveService.callReserve(Mockito.any(ReserveDetails.class))).thenReturn(reserveInfo);
          when(mockReserveService.callRelease(reserveInfo.getId())).thenReturn(releaseInfo);
          when(mockTransactionTemplate.execute(Mockito.&lt;TransactionCallback&lt;Reserve&gt;&gt;any())).thenReturn(reserveInfo);
          when(mockTransactionTemplate.execute(Mockito.&lt;TransactionCallback&lt;ReleaseInfo&gt;&gt;any())).thenReturn(releaseInfo);
          //Call the exe method
          someService.exe();
  
          verify(mockReserveService, times(1)).callReserve(Mockito.any(ReserveDetails.class));
          verify(mockReserveService, times(1)).callRelease(reserveInfo.getId())); 
          verify(mockSomeOtherService, times(1)).doSomthing());
  
      }
  </code></pre>
  
  <p>The second call to when(mockTransactionTemplate.execute.. with return object releaseInfo
  throws java.lang.ClassCastException: </p>