Spring + TestNG Autowiring failure - NOT due to "new"

361 views Asked by At

Having an issue with Spring Autowiring. I have an Integration Test class declared as follows:

@ContextConfiguration(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
public abstract class BaseIntegrationTest
        extends AbstractTestNGSpringContextTests {
    @Autowired
    protected TestProperties properties;
//... more stuff here
}

The Context configuration looks like this:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"com.ourcompany.myapp.it"},
        excludeFilters = @ComponentScan.Filter(value = com.inin.wfm.it.config.ConfigPackageExcludeFilter.class, type = FilterType.CUSTOM))
public class TestConfig {
    private TestProperties testProperties;
    private PropertyService propertyService;

    //This both creates and registers the bean with Spring
    @Bean
    public TestProperties getTestProperties() throws IOException {

        if (testProperties == null) {
            testProperties = new TestProperties(propertyService());
        }
        return testProperties;
    }

    @Bean
    public PropertyService propertyService() throws IOException {
        if (propertyService == null) {

            AppAdminConfig config = new AppAdminConfig.Builder(PropertyService.getEnvironment(), TestConfigKey.ApplicationId)
                .checkPropertyHasValue(GlobalConfigKey.KafkaBrokerList.key())
                .checkPropertyHasValue(GlobalConfigKey.ZookeeperList.key())
                .build();

            propertyService = new PropertyService(config.getPropertiesConfig());
            propertyService.initialize();
        }

        return propertyService;
    }
}

And this is the bean I'm having trouble with:

@Configurable
public class TestProperties {
    private PropertyService propertyService;

    public TestProperties(PropertyService propertyService) {
        this.propertyService = propertyService;
    }
    public String getCacheUri(){
        return propertyService.getPropertyRegistry().getString(TestConfigKey.CacheUri.key(), Default.CACHE_URI);
    }
}

I have multiple Test implementation classes that extend BaseIntegrationTest. All of them but one have valid references to their TestProperties field, but exactly one of the test implementation classes is getting a Null Pointer and throwing an NPE when an attempt is made to reference this.

So the question is, why is Spring @Autowire working fine for 3 different classes that extend the same base class, but wiring up null for the fourth? There is no additional configuration logic in any of the implementations.

For MCVE completeness, here's the bare bones of my impl class

public class CacheIT
        extends BaseIntegrationTest {
    @Test
public void testUserCache() throws InterruptedException, ExecutionException, TimeoutException {
        String uri = properties.getCacheUri() //TODO - NPE here
    }
}

I know there's not much to go on... I've been working with Spring for a long time and haven't seen it do this kind of thing before. According to everything I can see it should be working.

0

There are 0 answers