【问题标题】:How to populate database before running tests with Micronaut如何在使用 Micronaut 运行测试之前填充数据库
【发布时间】:2019-10-16 16:43:19
【问题描述】:

我正在寻找一种在执行我的测试类之前执行一些 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 脚本真的很棒。有没有办法在测试执行之前提供这样的*.sql 脚​​本?

【问题讨论】:

    标签: java micronaut


    【解决方案1】:

    TL;DR — 使用 Flyway。

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

    另一种解决方案是使用内存数据库(但对于实际应用程序,我会远离它)。例如,H2 可以指定一个“初始化”脚本和另一个用于数据播种(等等)的脚本。

    【讨论】:

    • 你读懂了我的想法。我正要为此尝试Flyway,但是,由于这只是一个实验项目,我想我会尝试这个H2“初始化”脚本(我不知道这个功能)。稍后我会尝试发布一个示例。
    猜你喜欢
    • 2014-08-25
    • 2018-08-27
    • 2013-04-25
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多