Trying to junit test a function using mockito which needs to call another function of same class, getting org.opentest4j.AssertionFailedError: null

Viewed 62

Let's say I have 2 function like this: (I don't want to share the actual code, it's pretty huge)

public class EmailService{

    String addressList1 = "test1@gmail.com,test2@gmail.com";
    public String[] getAddressList(String addressList){
        return addressList.split(",");
    }
    public int getNumber(){
        for (String address : getAddressList(addressList1))
        {
            if (address.equals("test1@gmail.com")){return 1;}
        }
        return 0;
    }
}

And I tried this for testing the getNumber() function:

EmailService emailService1 = new EmailService();
EmailService emailService2 = Mockito.spy(emailService1);
String deliveryAddress = "test1@gmail.com,test2@gmail.com";       
doReturn(deliveryAddress.split(",")).when(emailService2).getAddressList(deliveryAddress);

assertEquals(EmailService2.getNumber(), 1);

But I'm getting: org.opentest4j.AssertionFailedError: null

1 Answers

I found the answer for this.

Since addressList1 was a local variable, I had to set the value for this, before I call the getNumber function.

ReflectionTestUtils.setField(emailService2, "addressList1","test1@gmail.com,test2@gmail.com");
Related