Java开发者寻求Nebula集成测试中Seed info工厂与Assert的示例及最佳实践
Nebula集成测试:Seed数据生成与断言方案
核心思路
用Factory模式封装Nebula图数据库的测试数据生成逻辑,统一创建点、边等测试实体;测试时先通过Factory生成预设的Seed数据写入数据库,执行待测试逻辑后,查询数据库并断言结果是否符合预期。
代码示例
1. Seed数据Factory实现
import com.vesoft.nebula.client.graph.NebulaPoolConfig; import com.vesoft.nebula.client.graph.data.HostAddress; import com.vesoft.nebula.client.graph.data.ResultSet; import com.vesoft.nebula.client.graph.exception.IOErrorException; import com.vesoft.nebula.client.graph.net.NebulaPool; import com.vesoft.nebula.client.graph.net.Session; import java.util.ArrayList; import java.util.List; import java.util.Random; public class NebulaGraphSeedFactory { private final Session session; private final Random random = new Random(); public NebulaGraphSeedFactory(Session session) { this.session = session; } // 生成并写入用户点数据 public List<String> createUserNodes(int count) throws IOErrorException { List<String> userIds = new ArrayList<>(); for (int i = 0; i < count; i++) { String userId = "user_" + random.nextInt(10000); String cypher = String.format("INSERT VERTEX user(name, age) VALUES \"%s\":(\"%s\", %d)", userId, "测试用户" + i, 20 + random.nextInt(10)); session.execute(cypher); userIds.add(userId); } return userIds; } // 生成并写入用户关注边数据 public void createFollowEdges(List<String> userIds) throws IOErrorException { if (userIds.size() < 2) return; for (int i = 0; i < userIds.size() - 1; i++) { String cypher = String.format("INSERT EDGE follow(degree) VALUES \"%s\"->\"%s\":(95)", userIds.get(i), userIds.get(i + 1)); session.execute(cypher); } } }
2. 集成测试类示例
import com.vesoft.nebula.client.graph.exception.IOErrorException; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; public class NebulaIntegrationTest { private NebulaPool nebulaPool; private Session session; private NebulaGraphSeedFactory seedFactory; @Before public void setUp() throws Exception { // 初始化Nebula连接池与会话 NebulaPoolConfig poolConfig = new NebulaPoolConfig(); poolConfig.setMaxConnSize(10); nebulaPool = new NebulaPool(); nebulaPool.init(List.of(new HostAddress("127.0.0.1", 9669)), poolConfig); session = nebulaPool.getSession("root", "nebula", false); // 切换到测试空间 session.execute("USE test_space"); // 初始化Seed Factory seedFactory = new NebulaGraphSeedFactory(session); } @Test public void testFollowQuery() throws IOErrorException { // 1. 生成Seed数据 List<String> userIds = seedFactory.createUserNodes(3); seedFactory.createFollowEdges(userIds); // 2. 执行待测试查询(模拟业务查询逻辑) String query = String.format("GO FROM \"%s\" OVER follow YIELD dst(edge) as followed", userIds.get(0)); ResultSet resultSet = session.execute(query); // 3. 断言结果 resultSet.next(); assertEquals(userIds.get(1), resultSet.getString("followed")); } @After public void tearDown() throws Exception { // 清理测试数据(实际需遍历生成的userIds精准清理,此处为简化示例) for (String userId : seedFactory.createUserNodes(0)) { session.execute(String.format("DELETE VERTEX \"%s\"", userId)); } session.release(); nebulaPool.close(); } }
最佳实践
- 测试环境隔离:单独使用测试专用的Nebula空间,避免污染生产/开发环境数据。
- 数据清理策略:测试后务必清理Seed数据,可通过
DELETE语句、空间重建或使用Nebula事务回滚(需开启事务支持)。 - Factory复用与参数化:将Factory的生成逻辑参数化(如点属性、边类型可配置),适配不同测试场景。
- 精准断言:优先断言业务核心字段(如点ID、边属性),避免断言无关数据;批量数据可断言数量或关键集合匹配。
- 连接池复用:测试类中复用Nebula连接池,减少连接创建/销毁开销,提升测试效率。
- 异常捕获:在Factory和测试逻辑中捕获Nebula客户端异常,快速定位测试失败原因。
内容的提问来源于stack exchange,提问作者saber tabatabaee yazdi
相关产品推荐
相关产品推荐

