Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Junit - assert the list contains

I'm creating a List (ArrayList) that contains few elements. I want to make sure it contains the elements I added. now, this works only some of the times, since the order changes:

@Test
public void testThreeReporters(){
    ClientConfig myConfig = new ClientConfigFactory().getConfig().withMetricsReporters(new HashSet<>(Arrays.asList(ClientConfig.MetricsReporterType.LOG, ClientConfig.MetricsReporterType.GRAPHITE, ClientConfig.MetricsReporterType.CLOUD_WATCH)));
    List<ScheduledReporter> reporters = MetricsFactory.configureMetricsReporters(MetricsFactory.createMetricsClient(),myConfig);
    assertEquals(3, reporters.size());

    assertTrue(reporters.get(2) instanceof Slf4jReporter);
    assertTrue(reporters.get(1) instanceof GraphiteReporter);
    assertTrue(reporters.get(0) instanceof CloudWatchReporter);
}

I want to use 'contains' in order to not be depend on the order. I tried something like:

 assertTrue(Arrays.asList(reporters).contains((Arrays.asList(ClientConfig.MetricsReporterType.LOG, ClientConfig.MetricsReporterType.GRAPHITE, ClientConfig.MetricsReporterType.CLOUD_WATCH))));

and some other combinations, but it doesn't work.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

assertEquals(3, reporters.size());
assertTrue(reporters.stream().anyMatch(e -> e instanceof Slf4jReporter));
assertTrue(reporters.stream().anyMatch(e -> e instanceof GraphiteReporter));
assertTrue(reporters.stream().anyMatch(e -> e instanceof CloudWatchReporter));
about 4 years ago · Santiago Trujillo Relatório

0

There isn't really an easy way to do this with plain JUnit out of the box. You could develop a helper method to make this easy using the technique demonstrated by @JBNizet in his answer.

Or you could use an additional testing library that can help with this. For example using AssertJ you could do this:

import static org.assertj.core.api.Assertions.assertThat;

// ...

assertThat(reporters).extracting("class").contains(
    Slf4jReporter.class,
    GraphiteReporter.class,
    CloudWatchReporter.class
);

If you use Maven, you can add assertj dependency like this:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.4.1</version>
</dependency>
about 4 years ago · Santiago Trujillo Relatório

0

The problem is that you're trying to check if the reporters list contains a list, which it doesn't since you've specified it contains instances of ScheduledReporter. I've not used any of those classes, so I can't provide a more elegant solution, but the following should work:

@Test
public void testThreeReporters() {
    ClientConfig myConfig = new ClientConfigFactory().getConfig().withMetricsReporters(new HashSet<>(Arrays.asList(ClientConfig.MetricsReporterType.LOG, ClientConfig.MetricsReporterType.GRAPHITE, ClientConfig.MetricsReporterType.CLOUD_WATCH)));
    List<ScheduledReporter> reporters = MetricsFactory.configureMetricsReporters(MetricsFactory.createMetricsClient(),myConfig);
    assertEquals(3, reporters.size());

    assertTrue("No Slf4jReporter was found", containsReporter(reporters, Slf4jReporter.class));
    assertTrue("No GraphiteReporter was found", containsReporter(reportera, GraphiteReporter.class));
    assertTrue("No CloudWatchReporter was found", containsReporter(reporters, CloudWatchReporter.class));
}

/**
 * @return {@code true} if {@code reporters} contains an object of type
 *         {@code expectedClass}, otherwise {@code false}.
 */
private boolean containsReporter(List<ScheduledReporter> reporters, Class expectedClass) {
    for (ScheduledReporter reporter : reporters) {
        if (expectedClass.isInstance(reporter)) {
            return true;
        }
    }
    return false;
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda