@Test
public void testGetOnlyNewPartitions() throws Exception {
    setUp();
    new Expectations(){
        HiveUtil hiveUtil;
        {
            HiveUtil.getInstance(); returns(hiveUtil);
            hiveUtil.getAllpartitions(oldTable); returns(oldPartitions);
            hiveUtil.getAllpartitions(newTable); returns(newPartitions);
        }
    };
    PartitionFilter partitionFilter = new PartitionFilter(oldTable, newTable, HiveUtil.getInstance());
}

I am unit testing the class PartitionFilter, which uses a singleton class HiveUtil.

My test case is failing with the error "java.lang.IllegalStateException: Invalid context for the recording of expectations" while running. Any explanation on why this is happening?

This is the relevant part of my pom.xml:

<dependency>
         <groupId>org.jmockit</groupId>
         <artifactId>jmockit</artifactId>
         <version>1.13</version>
</dependency>

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
</dependency>

I have tried to put the jmockit dependency before the junit dependency in the pom. That didn't work.

Some more research suggested that I was not using the @RunWith(JMockit.class) annotation at the beginning of the class. However, when I tried to use it, I was presented with the error "Class cannot be resolved to a type". I made all the relevant imports.

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.*;

import mockit.*;
import mockit.integration.junit4.*;
import junit.framework.TestCase;

What am I doing wrong?

1

There are 1 best solutions below

1
On

Recent versions of JMockit (since version 1.7) require the use of a mocking annotation to introduce a mocked type/instance. Also, local mock fields are no longer supported. So, the test should be written as follows:

@Test
public void getOnlyNewPartitions(@Mocked final HiveUtil hiveUtil) throws Exception {
    setUp();

    new Expectations() {{
        hiveUtil.getAllpartitions(oldTable); result = oldPartitions;
        hiveUtil.getAllpartitions(newTable); result = newPartitions;
    }};

    PartitionFilter partitionFilter = 
        new PartitionFilter(oldTable, newTable, HiveUtil.getInstance());
}