jeudi 1 août 2019

Test uses internal ContextConfiguration of other test

I created a new test in my Project. For this one I used @ContextConfiguration with an internal Configuration class in the same class as the Test. But now my other tests are failing because they are using the configuration of the new test. How is this possible, I thought it is not possible to use a configuration inside of a test class from outside.

When I remove the internal configuration from the new test every other test works finde again.

@DataJpaTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ContextConfiguration(classes = EventServiceTest.Config.class)
class EventServiceTest {
    @Configuration
    @Import({WorkingTimeConfig.class,
             PartnerConfig.class,
             ProjectConfig.class,
             UserConfig.class,
             AccountGroupConfig.class,
             LanguageConfig.class,
             CountryConfig.class,
             EventConfig.class,
             LanguageConfig.class})
    static class Config {
        @SuppressWarnings("unused")
        @MockBean(reset = MockReset.BEFORE)
        private UserAttendanceBoard userAttendanceBoard;

        @Bean
        public PasswordEncoder passwordEncoder() {
            return PasswordEncoderFactories.createDelegatingPasswordEncoder();
        }

        @Bean
        public ImpersonateProperties impersonateProperties() {
            return new ImpersonateProperties();
        }
    }
...
}

Now this Test is not working:

@Import(MailSenderAutoConfiguration.class)
@DataJpaTest
@Transactional
public class ServiceTimeEntryServiceTest {

    private ServiceTimeService serviceTimeService;
    private ServiceTimeEntryRepository repository;

    @Autowired
    public ServiceTimeEntryServiceTest(ServiceTimeService serviceTimeService, ServiceTimeEntryRepository repository) {
        this.serviceTimeService = serviceTimeService;
        this.repository = repository;
    }

    @Test
    void getAllByAccountId() {...}

This error is thrown if I try to start my old tests:

org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'passwordEncoder' defined in class path resource [de/hlservices/timetracking/api/business/event/EventServiceTest$Config.class]: Cannot register bean definition

Thanks for your help :)

Aucun commentaire:

Enregistrer un commentaire