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

Comments

@borisbrodski
Copy link

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
Copy link

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

@borisbrodski
Copy link
Author

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
Copy link

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

@borisbrodski
Copy link
Author

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
Copy link

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 self-assigned this Sep 15, 2014
@rliesenfeld
Copy link
Member

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
Copy link

This is working with JMockit 1.11. Thank you.

@rliesenfeld
Copy link
Member

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
Labels
Development

No branches or pull requests

3 participants