【问题标题】:How to mock up the EntityManager using jmockit's Mockups API?如何使用 jmockit 的 Mockups API 模拟 EntityManager?
【发布时间】:2015-03-27 02:11:04
【问题描述】:

我在模拟 EntityManager 时遇到问题。一切都编译了,测试运行了,但是模拟的方法返回 null。

当我在模拟的“查找”方法中设置断点时,应用程序永远不会在那里暂停。 我设法以这种方式成功地使用静态方法模拟了不同的类 - 但是使用这个我遇到了问题。

我使用 Jmockit 1.7 和 Java 1.8.0。 我要模拟的类是:javax.persistence.EntityManager

如果需要更多信息 - 请询问。如果有任何帮助,我将不胜感激。

这是我的代码:

@RunWith(JMockit.class)
public class ShapefileSerializerTest {

    @Mocked
    private EntityManager em;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        new MockDatabase();
    }

    @Test
    public void testPrepareShapefile() {
        String[][] data = new String[][] {{"1", "100"}, {"1", "101"}, {"1", "102"}, {"1", "103"}, {"2", "200"}, {"2", "201"}};

        List<Map<String, String>> featuresData = Stream.of(data).map(row -> {
            Map<String, String> map = new HashMap<>(2);
            map.put("layerId", row[0]);
            map.put("id", row[1]);
            return map;
        }).collect(Collectors.toList());

        ShapefileSerializer shapefileSerializer = new ShapefileSerializer("shapefiles");
        // if I do not set up the em here - then it will be null inside the tested class
        Deencapsulation.setField(shapefileSerializer, em);

        Response response = shapefileSerializer.prepareShapefile(featuresData);

        assertEquals(Status.OK.getStatusCode(), response.getStatus());
    } 

    public static final class MockDatabase extends MockUp<EntityManager> {
        @Mock
        @SuppressWarnings("unchecked")
        public <T> T find(Class<T> entityClass, Object primaryKey) {
            return (T) new ProjectLayer();
        }
    }
}

【问题讨论】:

    标签: java unit-testing testing jmockit mockups


    【解决方案1】:

    将 JMockit 升级到 1.8 或更高版本,并将您的测试类更改为以下内容:

    @RunWith(JMockit.class)
    public class ShapefileSerializerTest {
        @Mocked EntityManager em;
    
        @Test
        public void testPrepareShapefile() {
            String[][] data = ...
            List<Map<String, String>> featuresData = ...
    
            ShapefileSerializer shapefileSerializer = new ShapefileSerializer("shapefiles");
            Deencapsulation.setField(shapefileSerializer, em);
    
            new Expectations() {{
                em.find((Class<?>) any, any); result = new ProjectLayer();
            }};
    
            Response response = shapefileSerializer.prepareShapefile(featuresData);
    
            assertEquals(Status.OK.getStatusCode(), response.getStatus());
        } 
    }
    

    【讨论】:

    • 感谢您的帖子。不幸的是,这不起作用。我从期望块中得到以下异常: IllegalArgumentException: Invalid type descriptor: (Ljava/lang/Class;Ljava/lang/Object;)java/lang/Object;此行导致错误:result = new ProjectLayer();我之前尝试过这个并且得到了同样的异常。
    • @MarcinRoguski 这是 1.7 版中的一个错误,已在 1.8 中修复。因此,您需要将 JMockit 升级到 1.8 或更高版本。
    • @Rogério 谢谢,这解决了 Expectations 的问题 :) 但是我仍然不知道为什么模拟 EntityManager 不起作用。我也尝试过另一堂课,但还是一样。未调用模拟方法。即使它被实例化并使用以下方法传递给测试类: Deencapsulation.setField(shapefileSerializer, em);
    • @MarcinRoguski 因为MockUps 与@Mocked 字段无关。另外,mock-up Java 接口(而不是类)时,需要调用getMockInstance()并使用返回的实例,否则无效。
    猜你喜欢
    • 2014-12-17
    • 2018-09-19
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2021-02-01
    相关资源
    最近更新 更多