1

Test Class

public class CollectionImplementationUnitTest {

  CollectionImplementation colImp;

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

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

  @Test 
  public void testgetinfo (){
    ...
    Mockito.when(
      colImp.historyFromStrgyTable(
         Mockito.anyString(),Mockito.anyString(),Mockito.anyString()
      )
    )
    .thenReturn(mockHistoryFromStrgyTable());

    CollectionsAccount Info = colImp.accountInfo(
      "string1","string2","string3", new IdentityAcc(), TableLst
    );

    //sometestmethods and asserts
  }
}

Class under Test

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.

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
Random
  • 909
  • 1
  • 9
  • 14
  • Hint: please study java coding style guidelines. Your code looks like you tried to basically violate each and any rule/convention on how variable/method names should look like ;-( – GhostCat Jul 30 '16 at 18:37
  • Hey @GhostCat Sorry about that. Do you want me to rename the classes, methods and variables. Or can you help me with doing that. [link](http://stackoverflow.com/questions/38484623/create-a-junit-test-case-for-a-method-which-contains-a-call-to-another-method-fr?noredirect=1#comment64376341_38484623) with the same problem. My code looks some what like this. You tried to help me before also. Thanks man – Random Jul 30 '16 at 18:40
  • Variable names should be camelCase. Then: no "_" in java names (except for ALL_UPPERCASE_CONSTANTS). Method names start lowerCase, too. – GhostCat Jul 30 '16 at 18:42
  • Hey I have changes my naming convention for methods and variables. Thanks for letting me know about the naming conventions. – Random Jul 30 '16 at 19:02
  • The code that you provide is invalid Java code. Could you please check the code of `CollectionImplementation` and `DetailsHelper`. – Stefan Birkner Jul 30 '16 at 20:14
  • The code is not complete @StefanBirkner. I have just written the methods that are being used for the test case testgetinfo(). Any suggestions? – Random Jul 30 '16 at 21:41
  • @GhostCat I tried to change the code btw according to your suggestion but the its failing the tests. [link] (http://stackoverflow.com/questions/38594496/error-when-trying-to-write-a-dependency-injection). Please help me if you have time – Random Jul 30 '16 at 21:55
  • @Random The signature of the `accountInfo` methods is not a valid java code. – Stefan Birkner Jul 30 '16 at 23:27

1 Answers1

2

This is happening because you're using a spy, not a mock. Running the "real" method when you call it is exactly what Mockito spies are supposed to do.

To stub your spy, this is the syntax that you want to use.

Mockito.doReturn(mockHistoryFromStrgyTable()).when(colImp).
    historyFromStrgyTable(Mockito.anyString(),Mockito.anyString(),Mockito.anyString());

You can find more detail on this in my post here.

Community
  • 1
  • 1
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110