【发布时间】:2020-05-11 23:29:01
【问题描述】:
我正在为我的示例应用程序构建一些集成测试,并且想知道是否可以在我的测试本身中创建一些测试数据,然后将其注入到正在运行的服务器中。我不想模拟我的数据,因为我希望我的测试贯穿整个堆栈。
我了解 Spring Boot 文档说服务器和测试在 2 个单独的线程中运行,但是否可以通过相同的上下文?
我目前的代码:
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ArtistResourceTests {
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private ArtistRepository artistRepository;
@Test
@Transactional
public void listReturnsArtists() {
Artist artist = new DataFactory().getArtist();
this.artistRepository.save(artist);
ParameterizedTypeReference<List<Artist>> artistTypeDefinition = new ParameterizedTypeReference<List<Artist>>() {};
ResponseEntity<List<Artist>> response = this.restTemplate.exchange("/artists", HttpMethod.GET, null, artistTypeDefinition);
assertEquals(1, response.getBody().size());
}
}
但这会返回 0 个结果而不是 1 个结果。
【问题讨论】:
标签: java spring spring-boot junit junit5