3
@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?

Nikhil Gupta
  • 31
  • 1
  • 3
  • What is this import trying to do? import static mockit.Mockit.*; I don't see a Mockit class in the mockit package of the JMockIt API – Saifuddin Merchant Dec 18 '14 at 06:05
  • My bad. That was a suggestion I was trying. Removed that. – Nikhil Gupta Dec 18 '14 at 07:11
  • which line gives you the class cannot be resolved to a type? are you seeing the dependencies under maven library in eclipse? – Saifuddin Merchant Dec 18 '14 at 08:09
  • also the way you have written the test case is a little odd. What's the returns for? They keyword result is used to mark a return value, new NonStrictExpectations() { { d.toString(); result = "mocked date"; } }; – Saifuddin Merchant Dec 18 '14 at 08:27
  • I used the returns after reading its use from this link : http://jmockit.googlecode.com/svn-history/r224/trunk/www/tutorial/AnExample.html And the class cannot be resolved to a type is received in the line @RunWith(JMockit.class) – Nikhil Gupta Dec 19 '14 at 11:05

1 Answers1

1

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());
}
Rogério
  • 16,171
  • 2
  • 50
  • 63
  • 1
    This works. But I get the following message in my console : "WARNING: JMockit was initialized on demand, which may cause certain tests to fail; please check the documentation for better ways to get it initialized." – Nikhil Gupta Dec 19 '14 at 10:53