gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:39:28 24 4
gpt4 key购买 nike

这是 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");

在 Debug模式下,我清楚地看到 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 没有任何电影。为什么有区别?代码与测试中的相同。

最佳答案

我不熟悉 neo4j,但我猜测测试是在 @Transaction 中运行的,而命令行运行器执行的代码则不然。

因此,将逻辑从命令行运行程序删除到可以封装在事务中的某个位置,无论是服务类还是应用程序监听器。

关于java - Springboot 测试与 commandLineRunner 给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57217547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com