Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thrown UnexpectedInvocation exception turns partially mocks into strict mocks #66

Closed
borisbrodski opened this issue Sep 15, 2014 · 8 comments
Assignees
Labels
bug

Comments

@borisbrodski
Copy link

@borisbrodski borisbrodski commented Sep 15, 2014

JMockit: 1.10

Here is a code snippet

import java.math.BigDecimal;

import mockit.NonStrictExpectations;
import mockit.internal.UnexpectedInvocation;

import org.junit.Test;

public class Test1 {
    @Test
    public void test1() {
        new NonStrictExpectations(BigDecimal.class) {{
            new BigDecimal(0).intValue();
            result = 1;
            maxTimes = 2;
        }};

        BigDecimal bigDecimal = BigDecimal.valueOf(0); // Invocation OK
        bigDecimal.intValue();
        bigDecimal.intValue();

        BigDecimal.valueOf(0);  // Invocation still OK

        try {
            bigDecimal.intValue();
        } catch (UnexpectedInvocation e) {

        }

        BigDecimal.valueOf(0);  // Unexpected invocation!!
    }
}

As you can see, til UnexpectedInvocation was thrown the BigDecimal class was partially mocked. But after UnexpectedInvocation was thrown BigDecimal turns into non-partially and also strict mocked class.

@Varun200864

This comment has been minimized.

Copy link

@Varun200864 Varun200864 commented Sep 15, 2014

Exception is thrown by third call of bigDecimal.intValue().

@borisbrodski

This comment has been minimized.

Copy link
Author

@borisbrodski borisbrodski commented Sep 15, 2014

Yes, but this exception is catched and ignored. If you remove the last line of the test

BigDecimal.valueOf(0); // Unexpected invocation!!

the test will be green.

@Varun200864

This comment has been minimized.

Copy link

@Varun200864 Varun200864 commented Sep 15, 2014

You are right.
If you comment third call of bigDecimal.intValue() then also it is green.

@borisbrodski

This comment has been minimized.

Copy link
Author

@borisbrodski borisbrodski commented Sep 15, 2014

Of course you are right:)

But still I would consider this a bug. In all other cases I was able to catch JMockit exceptions and proceed just normally.

Background: Currently I'm working on the JMockit-Xtend bridge (https://github.com/borisbrodski/jmockit-xtend). In order to test it, I have to do such "crazy" things, like producing errors and then catch the JMockit exceptions.

@Varun200864

This comment has been minimized.

Copy link

@Varun200864 Varun200864 commented Sep 15, 2014

I agree with you, This may be a bug.
The test is green if you change the code to

final BigDecimal bigDecimal1 = new BigDecimal(0);
new Expectations(BigDecimal.class) {{
bigDecimal1.intValue();
result = 1;
maxTimes = 2;
}};

@rliesenfeld rliesenfeld added the bug label Sep 15, 2014
@rliesenfeld rliesenfeld self-assigned this Sep 15, 2014
@rliesenfeld

This comment has been minimized.

Copy link
Member

@rliesenfeld rliesenfeld commented Sep 15, 2014

Yes, the test fails on JMockit 1.10, indicating a bug. But on JMockit 1.11, it passes, so I suppose this is already fixed.

Anyways, thanks for the report!

@Varun200864

This comment has been minimized.

Copy link

@Varun200864 Varun200864 commented Sep 16, 2014

This is working with JMockit 1.11. Thank you.

@rliesenfeld

This comment has been minimized.

Copy link
Member

@rliesenfeld rliesenfeld commented Sep 21, 2014

I closed this too soon; there was indeed a bug to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.