0

Below is my JUnit test code where I'm getting Null pointer Exception

public class MybatisApplicationTests {
    @Autowired
    UserService userService;
    @Test
    public void deleteUserByIdTest() {
        UserController uc = new UserController();
    Long id = 3L;
    assertEquals(1,uc.deleteUserById(id));

    }

Below is my controller UserService.java

@DeleteMapping("/deleteUserById/{id}")
public int deleteUserById(@PathVariable("id") Long id) {
    System.out.println("delete meth called");
    userService.deleteUserById(id);
    return 1;
}

And below is my ServiceImpl.java

@Override
public int deleteUserById(Long id) {
    System.out.println("delete meth service called");
    userMapper.deleteUserById(id);
    return 1;
}

I've tried in all possible ways, but I'm getting NullPointerException as an error. Help me to resolve the error & thanks in advance.

Naveen
  • 61
  • 1
  • 8
  • Share the error message that shown in console. – Mazedul Islam Jun 06 '21 at 10:57
  • Below is the error message java.lang.NullPointerException at com.naveen.controller.UserController.deleteUserById(UserController.java:54) at com.naveen.MybatisApplicationTests.deleteUserByIdTest(MybatisApplicationTests.java:31) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) – Naveen Jun 06 '21 at 11:00
  • You did not use `@Autowired` before `UserController uc;` in your `MybatisApplicationTests` class. – Mazedul Islam Jun 06 '21 at 11:14
  • @MazedulIslam I've added Autowired to UserController uc, still same Exception occurs, – Naveen Jun 06 '21 at 11:18
  • what is there in this line `UserController.java:54`? – Mazedul Islam Jun 06 '21 at 11:23
  • @MazedulIslam userService.deleteUserById(id); – Naveen Jun 06 '21 at 11:25
  • For what i see you have autowired UserController and then you overriden it with `this.uc = new UserController();` – Maciej Niedźwiedź Jun 06 '21 at 11:26
  • @MaciejNiedźwiedź I was trying to resolve that NullPointerException there. If you help me to resolve, it'll be much helpful. – Naveen Jun 06 '21 at 11:31
  • 2
    You should just autowire the UserController instead of using new operator as the services within that needs to be autowired. – deepakchethan Jun 06 '21 at 11:32
  • Can you provide us with configuration of Spring context you are using? – Maciej Niedźwiedź Jun 06 '21 at 11:34
  • @deepakchethan Yeah, You're right. issue resolved now – Naveen Jun 06 '21 at 11:36

1 Answers1

2

You should ideally autowire the UserController instead of using new operator as the services/beans within that controller needs to be autowired.

public class MybatisApplicationTests {
    @Autowired
    UserController uc;
    @Test
    public void deleteUserByIdTest() {
       Long id = 3L;
       assertEquals(1,uc.deleteUserById(id));
    }

But if this is a unit test, it is better to mock the inner bean functionality altogether by using Inject mocks and mock.

@RunWith(MockitoJUnitRunner.class)
public class MybatisApplicationTests {

    @InjectMocks
    UserController uc;
    @Mock
    UserService userService;
    @Test
    public void deleteUserByIdTest() {
       Long id = 3L;
       assertEquals(1,uc.deleteUserById(id));
    }
deepakchethan
  • 5,240
  • 1
  • 23
  • 33
  • So, what this inject mock and mock will do here? – Naveen Jun 06 '21 at 11:42
  • 1
    Basically instead of using "real" beans or services. We use mock beans that mock the service functionality. We can isolate and test the controllers code alone assuming that the bean is unit tested separately. But its all upto your requirements. – deepakchethan Jun 06 '21 at 11:43