0

I'm building a jar-with-dependencies that contains springboot classes and my test-classes too. my goal is to run my unit-test with Junit runner like this:

 java -cp my-jar-with-dependencies.jar org.junit.runner.JUnitCore com.my.AppTest

heres a code:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class AppTest{

@Autowired
protected Serve1 app;

@Test
public void shouldAnswerWithTrue()
{
    if(app == null){
        System.out.println("IT IS NULL !!!!!!!");
    }
    else{
        System.out.println(app.getB()); //prints BBBB!!!
    }

}

When running this within maven surefire - all is great - (BBBB!!!) but when running with org.junit.runner.JUnitCore as described above, the @Autowired app has not being injected and I got: IT IS NULL !!!!!!!

Einav
  • 1
  • 2
  • 1
    Of course. If Spring isn't creating the component, Spring won't inject dependencies into the component. If possible, create the a Serve1 yourself in an `@Before setUp()` method, but if it has transitive dependencies you'll have to have a way to create and inject those yourself (preferably with constructor injection rather than field injection). – David Conrad Feb 19 '19 at 20:46
  • The solution for the problem is here: https://stackoverflow.com/questions/52188683/running-spring-tests-from-executable-jar – Einav Feb 21 '19 at 19:33

0 Answers0