0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Aula

Dispositivos de Teste e Código de Teste Partilhado

Use o plugin java-test-fixtures para partilhar auxiliares, construtores e simuladores de teste reutilizáveis entre módulos sem os expor ao código de produção.

Dispositivos de Teste e Código de Teste Partilhado é uma aula grátis de Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Groovy & Gradle: JVM Automation and Build Engineering, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

The Shared Test Code Problem

Multiple modules often need the same test helpers (builders, fakes, fixtures). Copy-pasting them is brittle, and putting them in main code ships test-only classes to production.

Enter Test Fixtures

Gradle ships the java-test-fixtures plugin, which adds a dedicated testFixtures source set whose code can be consumed by other modules tests.

plugins {
    id("java-library")
    id("java-test-fixtures")
}

Source Set Layout

The plugin creates src/testFixtures/java. Classes there are compiled separately and packaged into a fixtures artifact.

  • src/main production
  • src/test tests
  • src/testFixtures shared test helpers

Writing a Fixture

Place reusable builders in the fixtures source set. They can depend on the main classes of the module.

public class UserBuilder {
    public static User aUser() {
        return new User("test", 42);
    }
}

Using Fixtures Inside the Module

The modules own tests automatically see its fixtures, no extra configuration needed.

@Test
void usesFixture() {
    User u = UserBuilder.aUser();
    assertEquals(42, u.getId());
}

Consuming Fixtures Across Modules

Another module declares a dependency on the fixtures using the testFixtures() helper.

dependencies {
    testImplementation(testFixtures(project(":core")))
}

Fixture Dependencies

Fixtures can declare their own dependencies with the testFixturesImplementation configuration, kept separate from production deps.

dependencies {
    testFixturesImplementation("org.assertj:assertj-core:3.25.0")
}

Why Not Just Use main?

Putting test helpers in main leaks them into the published JAR and the production classpath, increasing size and risk. Fixtures keep them isolated.

Publishing Fixtures

The fixtures artifact can be published alongside the main JAR, so downstream consumers in other repositories reuse them too.

gradle publish

Comparison with a Separate Module

You could create a standalone test-helpers module, but fixtures avoid an extra module, keep helpers next to the code they support, and use a clear naming convention.

Best Practices

Keep fixtures focused:

  • Only put genuinely reusable helpers here
  • Avoid leaking fixtures into main accidentally
  • Name builders/fakes clearly

Quick Check

Test your understanding of test fixtures.

Recap

You learned test fixtures:

  • Apply java-test-fixtures
  • Put shared helpers in src/testFixtures
  • Consume with testFixtures(project(...))
  • Keeps test-only code out of production artifacts

Perguntas Frequentes

A aula “Dispositivos de Teste e Código de Teste Partilhado” é grátis?

Sim — o texto completo de “Dispositivos de Teste e Código de Teste Partilhado” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Groovy & Gradle: JVM Automation and Build Engineering, atualize para CoddyKit PRO. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.

O que vou aprender em “Dispositivos de Teste e Código de Teste Partilhado”?

Use o plugin java-test-fixtures para partilhar auxiliares, construtores e simuladores de teste reutilizáveis entre módulos sem os expor ao código de produção. Você pratica Groovy & Gradle: JVM Automation and Build Engineering com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Groovy & Gradle: JVM Automation and Build Engineering?

Nenhuma experiência prévia é necessária. Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Dispositivos de Teste e Código de Teste Partilhado”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Groovy & Gradle: JVM Automation and Build Engineering?

Sim. Cada aula de Groovy & Gradle: JVM Automation and Build Engineering inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Testes unitários e de integração
  2. Relatórios e filtragem de testes
  3. Cobertura de código e análise estática
  4. Dispositivos de Teste e Código de Teste Partilhado
← Voltar para Groovy & Gradle: JVM Automation and Build Engineering