-1

So I am trying to autowire my http object in my test class and I have tried to integrate with @SpringBootTest however my http object still remains null.

My test class looks like this.

//@RunWith(SpringRunner.class)
@SpringBootTest(classes=Http.class)
public class GetItemTests {
private static final Logger LOGGER = LoggerFactory.getLogger(GetItemTests.class);

@Autowired
private Http httpClass;
}

My SpringBootMain class looks like this

@SpringBootConfiguration
@SpringBootApplication
public class SpringBootMain implements CommandLineRunner {

@Bean
ResourceConfig resourceConfig() {
    return new ResourceConfig().registerClasses(Version1Api.class,TokenUtilityClass.class, Paypal.class);
}

@Override
public void run(String... args) throws Exception {
    //test.authenticationToken();
}

public static void main(String[] args) {
    SpringApplication.run(SpringBootMain.class);
}
}

I have tried running with the SpringRunner as well as this but I receive errors about failing to load the application context.

Edward Muldrew
  • 253
  • 1
  • 6
  • 20
  • When do you get the errors, is it when you run the spring boot app or the test? Could you post the error logs? – codeMan Feb 11 '20 at 11:27
  • 1
    Also curious, why would you want to autowire Http class in your test class? – codeMan Feb 11 '20 at 11:28
  • `@SpringBootTest` with `@RunWith(SpringRunner.class)` is useless. The `@Autowired` field cannot be null as it would fail with an error during startup not while running the test. – M. Deinum Feb 11 '20 at 11:52
  • You should create the real object for a class under test. You should not autowire – Hatice Feb 11 '20 at 12:05
  • @codeMan My error when a run the test is simple it is a Java null pointer exception. I want to autowire the HTTP class as it uses an external config class which populates some of the HTTP's values from the an application.properties file – Edward Muldrew Feb 12 '20 at 21:51

1 Answers1

0

Unless your Http class is annotated with @Component (meaning it is a bean maintained by the Spring IoC Container) you will not be able to @Autowire it in the way you wrote.

Please also post the exception you are getting and the implementation of your Http class so we can potentially provide further help.

Norbert Bicsi
  • 1,562
  • 2
  • 19
  • 33