【发布时间】:2023-04-10 22:08:01
【问题描述】:
我在使用自定义控制器对 Spring Data Rest 应用程序进行单元测试时遇到问题。
@RepositoryRestResource(path = "channels")
public interface ChannelsRepository extends PagingAndSortingRepository<Channel, Long> {
}
@RepositoryRestResource(path = "systems")
public interface SystemRepository extends PagingAndSortingRepository<System, String>{
}
@RestController
public class SystemController {
private String query = "select a.id as fromId, b.id as toId\n" +
"from channel a, channel b\n" +
"where ST_Distance_Spheroid(\n" +
"\ta.coordinates,\n" +
"\tb.coordinates,\n" +
"\t'SPHEROID[\"WGS 84\",6378137,298.257223563]'\n" +
") <= :criticalDistance and a.id != b.id";
@Autowired
private EntityManager manager;
@Autowired
private SystemRepository repository;
@RequestMapping(value="systems/{systemId}/graph", method = RequestMethod.GET)
public Map<BigInteger, List<BigInteger>> createNeighbourhsGraph(@PathVariable("systemId") String systemId,
@RequestParam(value = "distance") double distance) {
List<Object[]> results = manager.createNativeQuery(query).setParameter("criticalDistance", distance).getResultList();
Map<BigInteger, List<BigInteger>> map = new HashMap<BigInteger, List<BigInteger>>();
for (Object[] result: results) {
BigInteger fromId = (BigInteger) result[0];
if (map.containsKey(fromId)) {
map.get(fromId).add((BigInteger) result[1]);
}
else {
List<BigInteger> neighbours = new ArrayList<BigInteger>();
map.put(fromId, new ArrayList<BigInteger>(Arrays.asList(new BigInteger[]{(BigInteger) result[1]})));
}
}
return map;
}
}
我尝试通过发布数据来填充数据,但没有任何运气。似乎在单元测试中没有创建任何在运行应用程序时创建的映射,所以我得到的只是异常说没有映射到'system/...'等。
如您所见,我尝试通过存储库填充数据。而且它也不起作用,可能是一些事务问题,因为我可以在日志中看到插入语句,但数据库中没有任何内容。
我看到我可以提供一些 sql 脚本来填充数据,但这似乎很愚蠢,因为如果我们仍然使用 SQL 来完成这样的简单任务,为什么还要使用 ORM。
@Before
public void setup() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
channels.System system = new System("foo");
GeometryFactory gf = new GeometryFactory();
Channel[] channels = new Channel[]{
new Channel(
1,
system,
gf.createPoint(new Coordinate(55.7565, 37.6153)),
1,
Arrays.asList(new String[]{"м. Охотный ряд"})
),
new Channel(
1,
system,
gf.createPoint(new Coordinate(55.8079, 37.5808)),
1,
Arrays.asList(new String[]{"м. Дмитровская"})
),
new Channel(
1,
system,
gf.createPoint(new Coordinate(50.4451, 30.5182)),
1,
Arrays.asList(new String[]{"м. Театральная"})
),
};
channelsRepository.save(Arrays.asList(channels));
system.setChannels(Arrays.asList(channels));
systemsRepository.save(system);
// ClassLoader classLoader = getClass().getClassLoader();
// String systemAsJson = IOUtils.toString(classLoader.getResourceAsStream("system.json"));
// String channelsAsJson = IOUtils.toString(classLoader.getResourceAsStream("channels.json"));
// JSONParser parser = new JSONParser();
// JSONObject system = (JSONObject) parser.parse(systemAsJson);
//
// String result = mockMvc.perform(post("/systems")
// .accept(MediaType.APPLICATION_JSON).
// content(system.toJSONString())
// ).andReturn().getResponse().getContentAsString();
// System.out.println(result);
}
【问题讨论】:
-
你看过
@Sql吗? -
见this page,它用于在测试期间以编程方式将数据插入数据库。
标签: java spring spring-mvc spring-data-rest