Spring context doesn't load my class in unittest

238 Views Asked by At

I can't get my unit test to load my configuration class.

My test class is annotated:

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(profiles = "test")
@ContextConfiguration (classes = ClientConfiguration.class)
public class ClientConfigurationTest { ...

ClientConfiguration.class

@ConditionalOnProperty(value = "dirt.security.oauth2client", matchIfMissing = false)
@Configuration
@PropertySource(value = "classpath:oauth2client-${spring.profiles.active:local}.properties", ignoreResourceNotFound=true)
public class ClientConfiguration {

    static {
        SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
    }

    @Bean
    @ConfigurationProperties(prefix = "dirt.security.oauth2client.client")
    ClientCredentialsResourceDetails clientConfig() {
        return new ClientCredentialsResourceDetails() {
            @Override
            public String toString() {
                return ReflectionToStringBuilder.toString(this);
            }
        };
    }

    @Bean
    @ConfigurationProperties(prefix = "dirt")
    protected DirtClientConfig dirtClientConfig() {
        return new DirtClientConfig();
    }

    @Bean
    DirtRestTemplate restTemplate() {

        DirtRestTemplate dirtRestTemplate =
                new DirtRestTemplate(clientConfig(),
                        new DefaultOAuth2ClientContext(), dirtClientConfig());
        dirtRestTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
        return dirtRestTemplate;
    }
}

None of the 3 beans get instantiated, and when I call this, it gets a dependecy error on one of the other beans

@Test
public void clientConfig() {
    DirtRestTemplate results =
            (DirtRestTemplate)context
                    .getAutowireCapableBeanFactory()
                    .createBean(DirtRestTemplate.class,
                            AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,
                            true);
    assertNotNull(results);
}
0

There are 0 best solutions below