如何在使用Micronaut运行测试之前填充数据库

5

我正在寻找一种在执行我的测试类之前执行一些SQL脚本的方法。使用Spring,我可以轻松地在我的测试类(或测试方法)上注释@Sql注解。但是我没有找到任何特定的方式来在Micronaut中实现相同的功能。

我唯一找到的方法是在测试方法中手动以编程方式填充数据,但是,在我的经验中,有时您必须执行多个插入操作才能测试单个用例。

我已经想出了以下代码来测试REST控制器:

代码

@Validated
@Controller("/automaker")
public class AutomakerController {
    private AutomakerService automakerService;

    public AutomakerController(AutomakerService automakerService) {
        this.automakerService = automakerService;
    }

    @Get("/{id}")
    public Automaker getById(Integer id) {
        return automakerService.getById(id).orElse(null);
    }

    @Get("/")
    public List<Automaker> getAll() {
        return automakerService.getAll();
    }

    @Post("/")
    public HttpResponse<Automaker> save(@Body @Valid AutomakerSaveRequest request) {
        var automaker = automakerService.create(request);

        return HttpResponse
                .created(automaker)
                .headers(headers -> headers.location(location(automaker.getId())));

    }

    @Put("/{id}")
    @Transactional
    public HttpResponse<Automaker> update(Integer id, @Body @Valid AutomakerSaveRequest request) {
        var automaker = automakerService.getById(id).orElse(null);
        return Objects.nonNull(automaker)
                ? HttpResponse
                .ok(automakerService.update(automaker, request))
                .headers(headers -> headers.location(location(id)))
                : HttpResponse
                .notFound();
    }
}

测试

@Client("/automaker")
public interface AutomakerTestClient {
    @Get("/{id}")
    Automaker getById(Integer id);

    @Post("/")
    HttpResponse<Automaker> create(@Body AutomakerSaveRequest request);

    @Put("/{id}")
    HttpResponse<Automaker> update(Integer id, @Body AutomakerSaveRequest request);
}

@MicronautTest
public class AutomakerControllerTest {

    @Inject
    @Client("/automaker")
    AutomakerTestClient client;

    @Test
    public void testCreateAutomakerWhenBodyIsValid() {
        var request = new AutomakerSaveRequest("Honda", "Japan");
        var response = client.create(request);
        assertThat(response.code()).isEqualTo(HttpStatus.CREATED.getCode());
        var body = response.body();
        assertThat(body).isNotNull();
        assertThat(body.getId()).isNotNull();
        assertThat(body.getName()).isEqualTo("Honda");
        assertThat(body.getCountry()).isEqualTo("Japan");
    }

    @Test
    public void testUpdateAutomakerWhenBodyIsValid() {
        var responseCreated = client.create(new AutomakerSaveRequest("Chvrolet", "Canada"));
        assertThat(responseCreated.code()).isEqualTo(HttpStatus.CREATED.getCode());
        var itemCreated = responseCreated.body();
        assertThat(itemCreated).isNotNull();
        var responseUpdated = client.update(itemCreated.getId(), new AutomakerSaveRequest("Chevrolet", "United States"));
        assertThat(responseUpdated.code()).isEqualTo(HttpStatus.OK.getCode());
        var itemUpdated = responseUpdated.body();
        assertThat(itemUpdated).isNotNull();
        assertThat(itemUpdated.getName()).isEqualTo("Chevrolet");
        assertThat(itemUpdated.getCountry()).isEqualTo("United States");
    }
}

我可以使用带有 @Before 注解的方法来填充所有需要的数据,但如果能像 Spring 那样使用 *.sql 脚本就好了。在测试执行之前提供这些脚本的方法是否存在?

1个回答

2

简而言之 — 使用Flyway。

使用Flyway,您可以非常轻松地设置和维护给定的数据库架构。对于您的情况,您放置在../test/resources/db/migration/(或任何其他默认位置)下的任何迁移脚本将仅对您的测试可见,并且可以在运行测试时自动执行/运行(如果配置)。

另一个解决方案是使用内存数据库(但我建议实际应用程序中不要使用)。例如,H2有一种指定“初始化”脚本以及用于数据填充等内容的方法。


你读懂了我的想法。我正准备尝试使用Flyway,但是由于这只是一个实验项目,我认为我会尝试使用H2的“初始化”脚本(我之前不知道这个功能)。稍后我会尝试发布一个示例。 - Leonardo Barbieri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接