samedi 30 juillet 2016

thenReturn in not working

Unittestingclass

public class CollectionImplementationUnitTest {

CollectionImplementation col_Imp;

  public void setup() throws Exception {
  ....
  col_Imp = Mockito.spy(new CollectionImplementation());
  ....
  }

  private String mockHistoryFromStrgyTable(){
        String  value1  =   "myValue";
        return  value1;
  }

 @Test 
  public void testgetinfo (){
  ......
  Mockito.when(col_Imp.HistoryFromStrgyTable(Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(   mockHistoryFromStrgyTable());
  CollectionsAccount Info = col_Imp.AccountInfo("string1","string2","string3", new IdentityAcc(), TableLst)
  //sometestmethods and asserts
  }

}

CollectionImplementation.class

    public class CollectionImplementation{
      .....
      @override
      public CollectionsAccount AccountInfo(("string1","string2","string3", new IdentityAcc(), TableLst)){
      DetailsHelper helper = new (db2, "string",getmethod());
      return helper.AccountInfo("string1","string2", new IdentityAcc(), TableLst); 
      }
     public String HistoryFromStrgyTable(){
  //contains a call to the data base
  }
    }

DetailsHelper

public class DetailsHelper{
  public CollectionsAccount AccountInfo((String string1,String string2,String string3, new IdentityAcc(), TableLst)){
  ...
  String paymentdetails = HistoryFromStrgyTable();  
  }
   public String HistoryFromStrgyTable(){
   //contains a call to the data base
   }
}

When I try to mock the data for the method HistoryFromStrgyTable() it is actually making a call to HistoryFromStrgyTable() instead of getting from mockHistoryFromStrgyTable().

My test cases are failing at this line Mockito.when(col_Imp.HistoryFromStrgyTable(Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn( mockHistoryFromStrgyTable());

Can anyone help me with this. I don't understand what's wrong. I also changed the method mockHistoryFromStrgyTable() from private to public since mockito cannot mock private methods.

Aucun commentaire:

Enregistrer un commentaire