title: Mockito stubbing is not working when service class method is called
body: <p>I tried to stub a set of data suppose to be return by a specific service method to a method present in controller so I need to stub for three methods call (getLopaReConfigPath,getBASeatKit and getBAColumnSeatKit) and it is working for only one method and not working for rest of the methods!!!</p>
  
  <p>Following is the code of Controller method to be tested</p>
  
  <pre><code>@RequestMapping(value = "/getSeatLayoutDisplay", method = RequestMethod.POST)
      @ResponseBody JSONObject getSeatLayoutDisplay(@RequestBody String params) {
  
          JSONObject resultJson = new JSONObject();
          JSONObject  combinedJson = new JSONObject();
  
          try {
              JsonObject gsonData = parseRequestDataToJson(params);
  
              StringBuffer filePathBuf = new StringBuffer(commonUtil.getLopaReConfigPath(gsonData.get("carrierName").getAsString(), gsonData.get("dbName").getAsString()));
              filePathBuf.append(LoadPropertyFiles.getInstance().getProperty("INPUT"));
  
              String seatKitXmlFilePath = filePathBuf.append(LoadPropertyFiles.getInstance().getProperty("XMLS")).toString();
              String columnXMl = filePathBuf.append(Constants.COLUMN_XML_FILENAME).toString();
  
              BASeatKitInfo seatKitInfo = lopaReConfigService.getBASeatKit(seatKitXmlFilePath);
              KitLayout columnSeatKitLeft = lopaReConfigService.getBAColumnSeatKit(gsonData.get("filepath").getAsString(),columnXMl);
  
              combinedJson.put("kitLayoutCenter", columnSeatKitLeft);
              combinedJson.put("kitInfo", seatKitInfo);
  
              resultJson.put("lopaReconfig",combinedJson);
  
          } catch (Exception e) {
              logger.error(" Failed due to " + e);
  
          }
          return resultJson;
  
      }
  </code></pre>
  
  <p>following is the test case I have returned </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(loader=AnnotationConfigWebContextLoader.class)
  @WebAppConfiguration
  public class LopaReConfigControllerTest {
  
  
      public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
  
      // Application dependency 
  
      private MockMvc mvc;
  
      @Mock
      LopaReConfigService lopaReConfigService;
  
      @Mock
      CommonUtil commonUtil;
  
      @Mock
      ProcessLopaReconfigFile processLopaReconfigFile;
  
      @Mock
      DBImportRepository dbImportRepository;
  
      @InjectMocks
      MockHttpSession session;
  
      @Autowired
      WebApplicationContext context;
  
      @InjectMocks
      LopaReConfigController lopaReConfigController;
  
      @Mock
      UserSessionPool sessionPool;
  
      @Before
      public void setup() {
  
          // Process mock annotations
          MockitoAnnotations.initMocks(this);
  
          this.mvc = MockMvcBuilders.standaloneSetup(lopaReConfigController).build();
      }
  
      @Test
      public void test_getSeatLayoutDisplay() throws Exception {
  
      // Construct JSON data to pass as parameter
  
          BASeatkitDataFeeder seatKitDataFeeder = new BASeatkitDataFeeder();
          Gson gson = new Gson();
          CommonUtil util = new CommonUtil();
          StringBuffer filePathBuff = new StringBuffer();
  
          filePathBuff.append(util.getLopaReConfigPath("ABC-300ER", "ABC-300ER-B777-v300B"));
          filePathBuff.append(LoadPropertyFiles.getInstance().getProperty("INPUT"));
  
      seatKitDataFeeder.setBuildVersion("Base");
      seatKitDataFeeder.setCarrierName("ABC-300ER");
      seatKitDataFeeder.setDbId("1558");
      seatKitDataFeeder.setDbName("ABC-300ER-B777-v300B");
      seatKitDataFeeder.setSessionId(session.getId());
      seatKitDataFeeder.setFilepath(filePathBuff.toString());
  
      String seatKitXmlFilePath = filePathBuff.append(LoadPropertyFiles.getInstance().getProperty("XMLS")).toString();
      String columnXMl = filePathBuff.append(Constants.COLUMN_XML_FILENAME).toString();   
  
      LopaReConfigRepositoryImpl lopaImpl = new LopaReConfigRepositoryImpl();
  
      BASeatKitInfo BAinfo = lopaImpl.getBASeatKit(seatKitXmlFilePath);
      KitLayout kitLayout = lopaImpl.getBAColumnSeatKit(seatKitXmlFilePath+Constants.SEATKIT_COLUMN_XML_FILENAME,columnXMl);
  
      //Stub the result whaen methods get called
  
          Mockito.when(commonUtil.getLopaReConfigPath("ABC-300ER", "ABC-300ER-B777-v300B")).thenReturn(filePathBuff.toString()); // working fine
          Mockito.when(lopaReConfigService.getBASeatKit(seatKitXmlFilePath)).thenReturn(BAinfo); // not stubbing the BAinfo object when method is called
          Mockito.when(lopaReConfigService.getBAColumnSeatKit("","")).thenReturn(kitLayout); // not stubbing the kitLayout object when method is called
  
          // Test method logic 
  
      mvc.perform(post("/getSeatLayoutDisplay")
          .contentType(MediaType.APPLICATION_JSON).content(jsonStub))
          .andExpect(status().isOk()) // After hitting specific URL/URI check the status code fall under 200 series
          .andExpect(content().contentType(APPLICATION_JSON_UTF8)); // Check the return statement is JSON
  
      // Verify that the the lopaReConfigService is called only once during execution.
  
      verify(lopaReConfigService, times(0)).getBASeatKit(seatKitXmlFilePath);
      verify(lopaReConfigService, times(0)).getBAColumnSeatKit(seatKitDataFeeder.getFilepath(),columnXMl);
      }
  }
  </code></pre>
  
  <p>The problem is I am trying to stub the objects for 3 methods but mockito setting the first method result properly when its get called but same thing is not happening for other two methods</p>
  
  <p>I have tried to do with Matcher and by overriding equals and hashcode methods but still it doesn’t helped me out.</p>
  
  <p>Any help will be appreciated thanks.   </p>
  
  <hr>
  
  <p>Stubbing works fine with following method</p>
  
  <pre><code> Mockito.when(commonUtil.getLopaReConfigPath("ABC-300ER", "ABC-300ER-B777-v300B")).thenReturn(filePathBuff.toString()); // working fine
  </code></pre>
  
  <p>Not working for following methods</p>
  
  <pre><code>Mockito.when(lopaReConfigService.getBASeatKit(seatKitXmlFilePath)).thenReturn(BAinfo); // not stubbing the BAinfo object when method is called
          Mockito.when(lopaReConfigService.getBAColumnSeatKit("","")).thenReturn(kitLayout); // not stubbing the kitLayout object when method is called
  </code></pre>