Springboot测试与commandLineRunner给出不同的结果

时间:2019-07-26 09:54:16

标签: java hibernate spring-boot neo4j

这是MVCE:https://github.com/neo4j-examples/movies-java-spring-data-neo4j

我添加到Person实体方法:

public void addMovie(Movie movie) {
    if (this.movies == null) {
        this.movies = new ArrayList<>();
    }
    this.movies.add(movie);
}

测试中,我添加了: 在设置中:

    keanu.addMovie(matrix);
    personRepository.save(keanu);

测试之一中:

Person p = personRepository.findByName("Keanu Reeves");

在调试模式下,我清楚地看到p取来时有电影收藏。

从github测试更改代码后,它看起来像这样:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
public class MovieRepositoryTest {
    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private PersonRepository personRepository;

    @Before
    public void setUp() {
        Movie matrix = new Movie("The Matrix", 1999, "Welcome to the Real World");

        movieRepository.save(matrix);
        Person keanu = new Person("Keanu Reeves", 1964);
        personRepository.save(keanu);
        Role neo = new Role(matrix, keanu);
        neo.addRoleName("Neo");
        matrix.addRole(neo);
        keanu.addMovie(matrix);
        personRepository.save(keanu);
        movieRepository.save(matrix);
    }

    /**
     * Test of findByTitle method, of class MovieRepository.
     */
    @Test
    public void testFindByTitle() {

        String title = "The Matrix";
        Movie result = movieRepository.findByTitle(title);
        Person p = personRepository.findByName("Keanu Reeves");

        assertNotNull(result);
        assertEquals(1999, result.getReleased());
    }

    /**
     * Test of findByTitleContaining method, of class MovieRepository.
     */
    @Test
    public void testFindByTitleContaining() {
        String title = "*Matrix*";
        Collection<Movie> result = movieRepository.findByTitleLike(title);
        assertNotNull(result);
        assertEquals(1, result.size());
    }

    /**
     * Test of graph method, of class MovieRepository.
     */
    @Test
    public void testGraph() {
        Collection<Movie> graph = movieRepository.graph(5);

        assertEquals(1, graph.size());

        Movie movie = graph.iterator().next();

        assertEquals(1, movie.getRoles().size());

        assertEquals("The Matrix", movie.getTitle());
        assertEquals("Keanu Reeves", movie.getRoles().iterator().next().getPerson().getName());
    }
}

但是,如果我这样做:

  @Bean
    CommandLineRunner demo(PersonRepository personRepository, MovieRepository movieRepository) {
        return args -> {
        personRepository.deleteAll();
        movieRepository.deleteAll();

        Movie matrix = new Movie("The Matrix", 1999, "Welcome to the Real World");

        movieRepository.save(matrix);

        Person keanu = new Person("Keanu Reeves", 1964);

        personRepository.save(keanu);

        Role neo = new Role(matrix, keanu);
        neo.addRoleName("Neo");

        matrix.addRole(neo);

        keanu.addMovie(matrix);
        personRepository.save(keanu);

        movieRepository.save(matrix);


        Movie result = movieRepository.findByTitle("The Matrix");
        Person p = personRepository.findByName("Keanu Reeves");
    };
}

我看到p没有任何电影。为什么会有所不同?代码与测试中相同。

1 个答案:

答案 0 :(得分:1)

我对neo4j不熟悉,但是我会猜测测试在@Transaction中运行,而命令行运行程序执行的代码却没有运行。

因此,将逻辑从命令行运行程序删除到可以包装在事务中的某个位置,即服务类或应用程序侦听器。