I have a problem with mocking up the EntityManager. Everything compiles, the test runs but the mocked method returns null.
When I set the breakpoint inside the mocked 'find' method the app is never suspended there. I managed to successfully mock different class with static methods this way - but with this one I have problems.
I use Jmockit 1.7 along with Java 1.8.0. The class I am trying to mock is: javax.persistence.EntityManager
If there are any more information needed - please ask. I would be very grateful for any help.
Here is my code:
@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();
}
}
}
Aucun commentaire:
Enregistrer un commentaire