Back to Repositories

Testing Neo4j Graph Relationships Implementation in spring-boot-demo

This test suite validates Neo4j graph database operations in a Spring Boot application, focusing on educational data relationships between students, teachers, and lessons. The tests verify core CRUD operations and complex relationship queries in a school management context.

Test Coverage Overview

The test suite provides comprehensive coverage of Neo4j graph database operations.

Key areas tested include:
  • Basic CRUD operations (save and delete)
  • Complex relationship queries between students and lessons
  • Student count aggregations by class
  • Classmate relationships through shared lessons
  • Teacher-student relationship mapping

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit4 integration. The tests demonstrate graph database querying patterns with varying depth parameters for relationship traversal. Implementation leverages Hutool JSON utilities for response formatting and Lombok for logging capabilities.

Notable patterns include stream operations for data transformation and relationship mapping using Neo4j’s depth-first traversal.

Technical Details

Testing stack includes:
  • JUnit4 for test execution
  • Spring Boot Test for dependency injection
  • Lombok @Slf4j for logging
  • Hutool JSONUtil for JSON processing
  • Neo4j embedded database for testing
  • Custom NeoService for database operations

Best Practices Demonstrated

The test suite exemplifies clean testing practices with clear separation of concerns and thorough validation of graph relationships.

Notable practices include:
  • Descriptive test method names
  • Comprehensive relationship testing
  • Proper logging of test results
  • Efficient data transformation using streams
  • Modular test organization by functionality

xkcoding/spring-boot-demo

demo-neo4j/src/test/java/com/xkcoding/neo4j/Neo4jTest.java

            
package com.xkcoding.neo4j;

import cn.hutool.json.JSONUtil;
import com.xkcoding.neo4j.model.Lesson;
import com.xkcoding.neo4j.model.Student;
import com.xkcoding.neo4j.service.NeoService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 测试Neo4j
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-12-24 15:17
 */
@Slf4j
public class Neo4jTest extends SpringBootDemoNeo4jApplicationTests {
    @Autowired
    private NeoService neoService;

    /**
     * 测试保存
     */
    @Test
    public void testSave() {
        neoService.initData();
    }

    /**
     * 测试删除
     */
    @Test
    public void testDelete() {
        neoService.delete();
    }

    /**
     * 测试查询 鸣人 学了哪些课程
     */
    @Test
    public void testFindLessonsByStudent() {
        // 深度为1,则课程的任教老师的属性为null
        // 深度为2,则会把课程的任教老师的属性赋值
        List<Lesson> lessons = neoService.findLessonsFromStudent("漩涡鸣人", 2);

        lessons.forEach(lesson -> log.info("【lesson】= {}", JSONUtil.toJsonStr(lesson)));
    }

    /**
     * 测试查询班级人数
     */
    @Test
    public void testCountStudent() {
        Long all = neoService.studentCount(null);
        log.info("【全校人数】= {}", all);
        Long seven = neoService.studentCount("第七班");
        log.info("【第七班人数】= {}", seven);
    }

    /**
     * 测试根据课程查询同学关系
     */
    @Test
    public void testFindClassmates() {
        Map<String, List<Student>> classmates = neoService.findClassmatesGroupByLesson();
        classmates.forEach((k, v) -> log.info("因为一起上了【{}】这门课,成为同学关系的有:{}", k, JSONUtil.toJsonStr(v.stream().map(Student::getName).collect(Collectors.toList()))));
    }

    /**
     * 查询所有师生关系,包括班主任/学生,任课老师/学生
     */
    @Test
    public void testFindTeacherStudent() {
        Map<String, Set<Student>> teacherStudent = neoService.findTeacherStudent();
        teacherStudent.forEach((k, v) -> log.info("【{}】教的学生有 {}", k, JSONUtil.toJsonStr(v.stream().map(Student::getName).collect(Collectors.toList()))));
    }
}