Search code examples
javaunit-testingspring-data-jpajmockit

Unable to mock interface with generic return type in Jmockit


I am trying to write a Junit unit test which mocks an interface that extends the spring-data-jpa interface CrudRepository<T, ID extends Serializable>. This interface provides the method

<S extends T> S save(S entity);

When I call this in my Expectations it wrongly records the return type as the type of the mocked interface rather than S (as resolved by my mocked interface) which causes a class cast exception.

I have debugged through and the generic signature it records is

<S:TT;>(TS;)TT;

There is a lot of code around figuring out what the return type is but eventually it comes up with the wrong answer. I'm fairly sure this is a bug but I hope someone can confirm this or tell me where I'm going wrong. One other thing to note is I am able to successfully mock other calls on the interface that just return T.

  • Jmockit: 1.16
  • JUnit: 4.12
  • spring-data-jpa: 1.7.1.RELEASE

Update with example

I have put together a scaled down test case that demonstrates the issue (below). In doing so I have uncovered a strange quirk. In the test I have two MyEntity objects which I use in the two expectations. If I change the save expectation to take and return the same objecct as the findOne call, the bug does not occur.

MyEntity.java

import javax.persistence.Entity;

@Entity
public class MyEntity
{
}

MyRepository.java

import org.springframework.data.repository.CrudRepository;

public interface MyRepository extends CrudRepository<MyEntity, Long>
{

}

DataAccess.java

public class DataAccess
{
    private final MyRepository repository;

    public DataAccess(MyRepository repository)
    {
        this.repository = repository;
    }

    public MyEntity resaveEntity(long id)
    {
        MyEntity entity = repository.findOne(id);
        return repository.save(entity);
    }
}

DataAccessTest.java

import mockit.Expectations;
import mockit.Mocked;
import org.junit.Test;

public class DataAccessTest
{

    @Test
    public void testResaveEntity(@Mocked final MyRepository repository) throws Exception
    {
        final MyEntity myEntity = new MyEntity();
        final MyEntity myEntity2 = new MyEntity();
        new Expectations()
        {
            {
                repository.findOne(1L);
                result = myEntity;

                repository.save(myEntity2);
                result = myEntity2;
            }
        };

        DataAccess dataAccess = new DataAccess(repository);
        dataAccess.resaveEntity(1);
    }
}

Exception

java.lang.ClassCastException: $Impl_MyRepository cannot be cast to MyEntity
at DataAccess.resaveEntity(DataAccess.java:16)
at DataAccessTest.testResaveEntity(DataAccessTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Solution

  • There is an error in the test. Where it reads

    repository.save(myEntity2);
    

    it should be

    repository.save(myEntity1);
    

    With this fix, the test should pass. On the other hand, that ClassCastException occurs because of a bug in JMockit, for which an issue was opened.