1

I'm pretty new to Junit and I'm trying to test isApproved method in Junit. I'm am mocking isDateValidOfData method using when and thenReturn using Mockito.when(isDateValidOfData(anyLong(), anyString()).thenReturn(true); This is getting an indexOutOfBounds exception. Here the serviceClass is being called on argument matchers so returning nothing in the list of data. I just want to know is there a way to mock the data and test it using Mockito and Junit. Using spy to get the object of the same class to call the method.

MyClass {
        //Service class calls the repository to fetch data. 
        @Autowired
        ServiceClass serviceClass; 

        public boolean isApproved(Long id, String code) {
                // Validation is done on the arguments
                //if id is of a particular type then return true by default
                //if code is in a list already present then continue with the below code or else return true by default. 
                return isDateValidOfData(Long id, String code);
        }

        public boolean isDateValidOfData(Long id, String code) {

                List<Data> data = serviceObject.getData(id, code);
                LocalDateTime eDate = data.get(0).getEDate();
                LocalDateTime rDate = data.get(0).getRDate();
                // check if eDate and rDate is greater than current date, if yes         return true or return false
        }
}

@RunWith(SpringRunner.class)
TestClass {
        @InjectMocks
        MyClass myClass;

        @Test
        public void isApprovedTest() {
                MyClass myClass1 = Mockito.spy(myClass);
                Mockito.when(myClass1.isDateValidOfData(anyLong(), anyString())).thenReturn(true);
                Assert.assertTrue(myClass1.isApproved(1234L, "1234");
        }       
}
Lemmy
  • 2,437
  • 1
  • 22
  • 30
Pi53
  • 211
  • 1
  • 4
  • 11
  • Could we see your exact test? – Shane Creedon Apr 05 '20 at 11:33
  • @ShaneCreedon, updated my question with test case. – Pi53 Apr 05 '20 at 11:46
  • @Pi53 Please [edit] your question to include the full source code you have. It is not know what `serviceObject()` is or how `isApproved()` is implemented. See [mcve]. Also include the full error message you get. – Progman Apr 05 '20 at 11:54
  • @Progman, I've tried to be descriptive in the comments. Hope that clarifies all the doubts. Nothing else code wise that relates to the problem. – Pi53 Apr 05 '20 at 12:02
  • @Progman, yes it does. Thanks a lot. Not sure why I hadn't tried it before. – Pi53 Apr 05 '20 at 13:09

1 Answers1

2

Since you are using @InjectMocks you should use @RunWith(MockitoJUnitRunner.class) on class level an everything will work fine.

If you want to use @RunWith(SpringRunner.class) then use @MockBean and @SpyBean to write test.

EDIT:

You can see more @RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class) here

Additional, you can check this Why we use Mockitojunitrunner class in our junit test?

and @RunWith(SpringRunner.class) with Spring Boot

Also, please check this one Mockito - difference between doReturn() and when()

Lemmy
  • 2,437
  • 1
  • 22
  • 30
  • it didn't solve my problem. Have changed it to use SpyBean annotation instead of InjectMocks but still getting indexoutofbounds exception. – Pi53 Apr 05 '20 at 12:54
  • can you update your question with the new code? Or add here the new code – Lemmy Apr 05 '20 at 12:59
  • 1
    Progman's comment has answered my question. The thread proved to be very helpful. Using doReturn instead of when thenReturn. – Pi53 Apr 05 '20 at 13:10