Link to home
Start Free TrialLog in
Avatar of buttonMASTER
buttonMASTER

asked on

JUnit 5 + Spring: @Autowired variable is null

Hi,

So I'm writing some tests in JUnit and am just learning the Java Spring Framework. I have a private variable in my test with the @Autowired annotation, but in my test fails because the variable is null.

Example:
public class ServiceTest
{
   @Autowired private MyService m_MyService;

   @Test
   public void testMyService()
   {
      assertTrue(m_MyService.doWork());
   }
}

Open in new window

So when I run the test, I get a NullPointerException on this line:
assertTrue(m_MyService.doWork());

Open in new window


Any ideas? Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial