Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call another method in mock object

I have object Panel inside is Methods

protected Confirmation confim() {
    Confirmation confirmation = new Confirmation() {
        @Override
        public void onConfirm() {
            doCancel();
        }
    };

    return confirmation;        
}

protected void doCancel() {
    ....
}

and i need to check That confirm() call doCancel Method

My Test

@Test
public void test() {
    Panel panel = Mockito.mock(Panel.class);

    Mockito.doCallRealMethod().when(panel).confirm();
    Mockito.doCallRealMethod().when(panel).cancel();

    panel.confirm();

    Mockito.verify(panel).cancel();
}`

The problem is that cancel() is never called... maybe it is because OnConfirm() is an overidden method and it takes a mock... or something... Can some one help how to test?

P.S. Panel must be Mocked.

problem was that o dont call panel.confirm().onConfirm(); this is stupid mistake.. so please remove this Question..

like image 406
x3mik Avatar asked Oct 31 '25 17:10

x3mik


1 Answers

 @Test
public void test() {
    Panel panel = Mockito.mock(Panel.class);

    Mockito.doCallRealMethod().when(panel).confirm();
    Mockito.doCallRealMethod().when(panel).cancel();

        panel.confirm().onConfirm();

    Mockito.verify(panel).cancel();
}`
like image 138
iamct Avatar answered Nov 04 '25 11:11

iamct



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!