Issue
How can I test my configuration without starting the whole Spring context class or should I do that?
@Configuration
public class GaRuConfig {
@Bean
public List<GaRu> rules(){
SSRule rule1 = new SSRule();
CSRule rule2 = new CSRule();
DGRule rule3 = new DGRule();
EGRule rule4 = new EGRule();
return List.of(rule1, rule2, rule3, rule4);
}
}
Is there a way to test this class?
Solution
Using AssertJ you can do the following:
public class GaRuConfigTest {
private GaRuConfig gaRuConfig = new GaRuConfig();
@Test
public void shouldReturnCorrectListOfRulesWhenRequested() {
// Act
List<GaRu> rules = this.gaRuConfig.rules();
// Assert
Assertions.assertThat(rules.stream().filter(rule -> SSRule.class == rule.getClass()).collect(Collectors.toList())).isNotEmpty();
Assertions.assertThat(rules.stream().filter(rule -> CSRule.class == rule.getClass()).collect(Collectors.toList())).isNotEmpty();
Assertions.assertThat(rules.stream().filter(rule -> DGRule.class == rule.getClass()).collect(Collectors.toList())).isNotEmpty();
Assertions.assertThat(rules.stream().filter(rule -> EGRule.class == rule.getClass()).collect(Collectors.toList())).isNotEmpty();
Assertions.assertThat(rules).hasSize(4);
Assertions.assertThat(SSRule.class == rules.get(0).getClass()).isTrue();
Assertions.assertThat(CSRule.class == rules.get(1).getClass()).isTrue();
Assertions.assertThat(DGRule.class == rules.get(2).getClass()).isTrue();
Assertions.assertThat(EGRule.class == rules.get(3).getClass()).isTrue();
}
}
Answered By - João Dias
Answer Checked By - Katrina (JavaFixing Volunteer)