Framework

Seed Spring Boot + Postgres with MockHero

Approach

Use a CommandLineRunner that only runs in a dev profile, pulls data from MockHero, and persists via JPA.

// src/main/java/com/example/seed/DevSeeder.java
@Component
@Profile("dev")
public class DevSeeder implements CommandLineRunner {

  @Autowired UserRepository users;
  @Autowired OrderRepository orders;

  @Override
  public void run(String... args) throws Exception {
    var body = Map.of(
      "tables", List.of(
        Map.of("name","users","count",50,"fields", List.of(
          Map.of("name","id","type","uuid"),
          Map.of("name","email","type","email")
        )),
        Map.of("name","orders","count",200,"fields", List.of(
          Map.of("name","id","type","uuid"),
          Map.of("name","userId","type","ref","ref","users.id"),
          Map.of("name","total","type","price")
        ))
      )
    );

    var client = HttpClient.newHttpClient();
    var req = HttpRequest.newBuilder()
      .uri(URI.create("https://api.mockhero.dev/api/v1/generate"))
      .header("x-api-key", System.getenv("MOCKHERO_API_KEY"))
      .header("Content-Type","application/json")
      .POST(BodyPublishers.ofString(new ObjectMapper().writeValueAsString(body)))
      .build();
    var res = client.send(req, BodyHandlers.ofString());
    // parse + persist
  }
}

Get Started

Free API key.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles