0

This is my Test class inside spring application.

class MyTest{
    @Inject
    private Configuration config;
    @Inject
    VUtils vUtils;

    @Test
     method1(){
     ....
        VUtilsP.method("test_property"); // VUtilsP is NULL here..
     }
   }

This below class is in jar (added as maven dependency to above)

@Component
Class VUtilsP{
    @Inject
    Configuration configD;

    public void method(String s){
    // do something
       configD.getConfig(s); 
    }
}

application directory :

MyService
    !- **src/test/java/MyTest.java**
    !- External libraries
       !- test-engine.jar
          !- **src/java/VUtilsP.java**

I want to inject Configuration object to VUtilsP class so I can access it inside VUtilsP class.

My approach is to convert VUtilsP to a bean and inject into MyTest but this is not working. VUtilsP is null. Please let me know what I'm missing here or suggest a different approach to get it work. Thank you so much.

CuriousToLearn
  • 153
  • 1
  • 12
  • What’s your component scan path? Is your external library within this path? – Boris the Spider Apr 21 '22 at 06:01
  • an autowired field cannot be null, the test would fail to start. So if it is null you aren't using Spring to start your test. – M. Deinum Apr 21 '22 at 06:20
  • If you see config object in MyTest that works fine in other tests in the same class.. That confirms my tests are using spring right? just my class(VUtilsP) that I'm trying to inject is null. VUtilsP is not managed by Spring but just adding @component should be working isnt it? – CuriousToLearn Apr 21 '22 at 06:31
  • use @ Autowired annotation instead of @ Inject – Shridutt Kothari Apr 21 '22 at 06:39
  • 2
    No it doesn't. The fact that you slap an annotation in a test doesn't mean it uses spring. If you don't use the extension (Junit5) or runner(Junit4) for spring, the test is totally unaware of Spring. As stated an autowired field cannot be `null`, the test wouldn't even execute and blow up with an error indicating it couldn't start the application context due to unresolvable dependencies. It doesn't, hence not using spring in your test (at least not to bootstrap your test). – M. Deinum Apr 21 '22 at 06:44
  • 1
    @ShriduttKothari Autowired and Inject are synonymous for Spring. – Sıddık Açıl Apr 21 '22 at 06:58
  • @RunWith(SpringRunner.class) class MyTest{ } Tried this but still didnt work. :( – CuriousToLearn Apr 21 '22 at 07:00
  • @CuriousToLearn as M. Denium has already stated you need to be using SpringExtension or SpringRumner depending on your JUnit version. [see this SO question for an example usage of extension](https://stackoverflow.com/questions/61433806/junit-5-with-spring-boot-when-to-use-extendwith-spring-or-mockito) – Sıddık Açıl Apr 21 '22 at 07:02
  • so are you saying even if this inject works "@Inject private Configuration config;" in myTest class, there are chances this might not use spring? I'm confused here. – CuriousToLearn Apr 21 '22 at 07:06
  • Only the runner won't work you also need to pass it a configuration. Spring injection without using Spring in your test is simply not going to work. I also highly doubt your other `@Inject` works as you say as in that case it still would blow up due to unresolved dependencies. Hence the code you have here is an actual dumbed down version of your code. Please show actual code and the framework versions you are using. – M. Deinum Apr 21 '22 at 07:15
  • post your complete code – Shridutt Kothari Apr 22 '22 at 03:49

0 Answers0