How do I make fun of a method with shared objects in JMockit?

This question alone explains if you know how to use JMockit: how do I mock a method that has generics in it? I want to mock this method: public T save (T entity), but it always throws an exception like this:

mockit.RealMethodNotFoundForMockException: Corresponding real methods not found for the following mocks:
Object save(Object)
    at mockit.internal.RedefinitionEngine.modifyRealClass(RedefinitionEngine.java:130)
    at mockit.internal.RedefinitionEngine.modifyRealClass(RedefinitionEngine.java:88)
    at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:72)
    at mockit.Mockit.setUpMocks(Mockit.java:197)
    at com.mytest.MyTest.setUp(AdminManagerImplTest.java:83)
+3
source share
4 answers

it works for me for example

package a;

import junit.framework.TestCase;
import mockit.Mockit;

public class JMockitCoreExampleTest extends TestCase {

    public class MockMe {
        public T save (T t) {
            return null;
        }
    }

    public void testDoOperationAbc () {
        Mockit.redefineMethods(MockMe.class, SayThatYouMockMe.class);
                MockMe mock = new MockMe();
        System.out.println(mock.save("aaa"));
    }

    public static class SayThatYouMockMe {
        public T save(T t) {
            return t;
        }
    }
}

Loaded external tool: mockit.integration.junit3.JUnitTestCaseDecorator
Loaded external tool: mockit.integration.junit4.JUnit4ClassRunnerDecorator
aaa

, ? , jmockit hibernate3emul.jar

+1

:

Java 1.7 Java 1.6, TestNG jMockit 1.5 1.16, in-method MockUp <... > .

:

public class MyCache extends AbstractLazyCache<ExchangeObject> {...}

:

 public abstract class AbstractLazyCache<CachedObject> extends AbstractCache {
      // ...

      protected void putCachedObject(String tenantId, String objectId, CachedObject o, Date expiryDate) {...}
  }

:

    new MockUp<MyCache>() {
        // invoked once manually, once on expired cache
        @Mock(invocations = 2)
        public void putCachedObject(Invocation inv, String tenantId, String objectId,
                RangeExchange o, Date expiryDate) {
            // expire one second in the future
            inv.proceed(tenantId, objectId, o, new Date(System.currentTimeMillis() + waitTimeMillis));
        }
    };

. , , , :

    new MockUp<MyCache>() {
        // invoked once manually, once on expired cache
        @Mock(invocations = 2)
        public <RangeExchange> void putCachedObject(Invocation inv, String tenantId, String objectId,
                RangeExchange o, Date expiryDate) {
            // expire one second in the future
            inv.proceed(tenantId, objectId, o, new Date(System.currentTimeMillis() + waitTimeMillis));
        }

    };
+2

, , , . , , JMockit , .

, , :

public <T> void foo(T param) {
  ...
}

:

public void foo(Object param) {
  ...
}

, -, , JMockit .

, :

public <T extends Map> foo(T param) {
  ...
}

:

public void foo(Map param) {
  ...
}

, , , mock , , , JMockit .

Read more about http://today.java.net/pub/a/today/2003/12/02/explorations.html

0
source

Perhaps you have one constellation:

I tried the Mock method <T> T merge(T t)in org.hibernate.ejb.EntityManagerImpland got the same Mockit error.

Mockit.redefineMethods(EntityManagerImpl.class, EntityManagerImplMock.class);

My mistake was that the merge method is NOT declared in org.hibernate.ejb.EntityManagerImpl, but in its superclass AbstractEntityManagerImpl. Now i have

Mockit.redefineMethods(AbstractEntityManagerImpl.class, EntityManagerImplMock.class);

from

public static class EntityManagerImplMock {
    public <T> T merge(final T o) {
        // nothing
        return o;
    }
}

and everything works well.

0
source

Source: https://habr.com/ru/post/1702593/


All Articles