1

I have a service which I am trying to inject across various classes in my tests but I am getting its instance as null.

My config interface:

MyService.java

public interface MyService {

    public String getHostUri();

}

My implementation class of this interface: MyServiceImpl.java

public class MyServiceImpl implements MyService {

    private static final String BASE_HOST_URI_CONFIG = "localhost:4444";

    @Override
    public String getHostUri() {
        return BASE_HOST_URI_CONFIG;
    }

My Spring configuration class with the bean:

@Configuration

public class AutomationSpringConfig {

    @Bean
    public MyService getMyService(){
        return new MyServiceImpl();
    }

}

My testNG class:

@ContextConfiguration(classes=AutomationSpringConfig.class ,loader =AnnotationConfigContextLoader.class)

public class BasicAutomatedTest extends AbstractTestNGSpringContextTests {

    private static final Logger LOGGER = Logger.getLogger(BasicAutomatedTest.class);

    @Inject
    private MyService myService;


    @Test
    public void basicTest {
        Setup setup = new Setup();
        LOGGER.info(myService.getHostUri());
        LOGGER.info(setup.myService.getHostUri());

    }
}

My helper class in which I am not able to get the injection:

public class Setup {

  @Inject

  public MyService myService;

}

So when I try to get the hostUri via the setup object in the BasicAutomatedTest's basicTest method I get a NullPointerException. So I am not able to inject the MyService bean in the Setup class.

Raj Hassani
  • 1,577
  • 1
  • 19
  • 26

1 Answers1

0

In order to use annotations you need to specify that behaviour in your beans XML configuration file. Something like this:

<context:component-scan base-package="your.base.package"/>
<context:annotation-config/>

Hope it helps!

Kikin-Sama
  • 425
  • 3
  • 8