Thrown UnexpectedInvocation exception turns partially mocks into strict mocks #66

Closed
borisbrodski opened this Issue Sep 15, 2014 · 8 comments

Comments

3 participants
@borisbrodski

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.

Show comment
Hide comment
@Varun200864

Varun200864 Sep 15, 2014

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

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

@borisbrodski

This comment has been minimized.

Show comment
Hide comment
@borisbrodski

borisbrodski 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.

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.

Show comment
Hide comment
@Varun200864

Varun200864 Sep 15, 2014

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

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

@borisbrodski

This comment has been minimized.

Show comment
Hide comment
@borisbrodski

borisbrodski 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.

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.

Show comment
Hide comment
@Varun200864

Varun200864 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;
}};

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.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Sep 15, 2014

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!

Member

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.

Show comment
Hide comment
@Varun200864

Varun200864 Sep 16, 2014

This is working with JMockit 1.11. Thank you.

This is working with JMockit 1.11. Thank you.

@rliesenfeld

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Sep 21, 2014

Member

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

Member

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