Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing previously defined expectations in JMockit

I have an object that I'm mocking with a JMockit NonStrictExcpection() in the @Before/setUp() method of my test class so that it returns the value expected for normal execution of my class under test.

This is fine for all of my test methods save for a single test where I want to test non-normal operation of this code.

I have tried creating a new expectation in the test method, which I believed would override the expectation in the setUp method, but I have found that the expectation in the setUp method is suppressing the new expectation.

When I remove the setUp expectation, the test method behaves as expected (but all my other tests fail, naturally).

How should I code my test class so that I can have the expectations correctly defined for each test with the minimum amount of code? (I know I could copy/paste the expectation code into each test method, but I don't want to do that if at all avoidable).

My test code looks something like this (note, this is sorta psuedocode and doesn't compile, but you get the idea):

public class TestClass{

    @Before
    public void setUp(){

        // Here I define the normal behaviour of mockObject
        new NonStrictExpectations() {{
            mockObject.doSomething();
            result = "Everyting is OK!";
        }};

        // Other set up stuff...

    }

    // Other Tests...

    /**
     * This method tests that an error when calling 
     * mockObject.doSomething() is handled correctly.
     */
    @Test(expected=Exception.class)
    public void testMockObjectThrowsException(){

        // This Expectation is apparently ignored...
        new NonStrictExpectations() {{
            mockObject.doSomething();
            result = "Something is wrong!";
        }};

        // Rest of test method...

    }
}
like image 597
chrisbunney Avatar asked Feb 10 '11 12:02

chrisbunney


People also ask

What is JMockit Expectations?

An expectation represents a set of invocations to a specific mocked method/constructor that is relevant for a given test. An expectation may cover multiple different invocations to the same method or constructor, but it doesn't have to cover all such invocations that occur during the execution of the test.

What is the difference between JMockit and Mockito?

Mockito uses 'proxy API' design architecture. 7. JMockit is based on Java 1.5 instrumentation API framework. Finally, the JMockit Testing Toolkit has a wider scope and more ambitious goals than other mocking toolkits, in order to provide a complete and sophisticated developer testing solution.

Is JMockit unit testing?

JMockit is open source software licensed under the MIT License. It includes APIs for mocking, faking, and integration testing, and a code coverage tool. The library is meant to be used together with a testing framework such as JUnit or TestNG.

What is NonStrictExpectations?

NonStrictExpectations() Registers one or more non-strict expectations recorded on available mocked types and/or mocked instances, as written inside the instance initialization body of an anonymous subclass or the called constructor of a named subclass.


1 Answers

I usually just make a private method which returns an Expectations type:

private Expectations expectTheUnknown()
{
    return new NonStrictExpectations()
    {{
        ... expectations ...
    }};
}

And then just call the method in the test methods that need the exact expectation:

@Test public void testUknown()
{
    expectTheUnknown();
    ... here goes the test ...
}
like image 97
Steen Avatar answered Nov 15 '22 09:11

Steen