2

I have a test class in which I have around 10 unit test cases. In this, I have a mock method created which is useful in just 2 of the 10 test cases.

public static class MockCreateStudent extends Mockup<CreateStudentDAO>{
    @Mock
    public boolean isFeesPaid(long studentID){
        return true;
    }
}

So technically I want the isFeesPaid() method to be executed as-is for 8 test cases and mocked for 2 of the test cases.

Is there any way this can be achieved?

2
  • 1
    That doesn't sound right - why do you only mock the collaborator under certain circumstances? Commented Jul 31, 2019 at 9:57
  • @jonrsharpe: I just mentioned a simplified version of my actual problem statement above. Practically the isFeesPaid() method takes 4 parameters and populating them is a tedious task. For the 2 test cases that I'm talking about, this method isn't significant. So I'm trying to make it simpler by mocking it. Makes sense? Commented Jul 31, 2019 at 10:20

1 Answer 1

2

Use a spy in that case instead of a mock. For most of the cases, it will invoke the real implementation of the collaborator and for those 2 cases, you use the given().willReturn() stubbing in the //Given section just before you invoke the SUT.

Here is more info on the spy annotation which I would recommend you use: link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.