0

I am new to Java, tried various ways to resolve this but there is no luck.

The TestFeedService interface object (testService) value is showing null on testService.sendMessage(dataBO, false) call. This is there inside the method I'm calling from the test case. I am not sure why this is happening. I tried creating Object in the setup() but still it is failing. Can someone please help me in this.

Below is the Test case code

public class testApiControllerTest {
private MockMvc mockMvc;
private TesApiController testApiController;
private TestFeedService testService;

@Before
public void setup() {
  testApiController = mock(TestApiController.class);
  testService = mock(TestFeedService.class);
  testService = new TestFeedServiceImpl();
}

@Test
public void testProcessMessage() {
  TestApiController testApiController = new TestApiController();
  TestForm2 testForm2 = new TestForm2();
  testForm2.setType("PROMO_CODE");
  testForm2.setUserId("1");
  testForm2.setMessage("Welcome");
  ResponseEntity<?> responseEntity = testApiController.processMessage(testForm2);
  assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
}}

Java Code:

  public class TestApiController {
@Autowired
private TestFeedService testService; // This is an interface 

@RequestMapping(path = "/api/process-message", method = RequestMethod.POST)
public ResponseEntity<?> processMessage(@RequestBody TestForm2 testForm) {
  DataBO dataBO = convertBO(testForm);
  testService.sendMessage(dataBO, false);  // here I am getting testService = null and causing exception
  return ResponseEntity.ok().build();
} // Some more code
Rahul Vel
  • 35
  • 1
  • 7
  • 2
    Why are you creating a new object with new TestFeedServiceImpl(); after mocking the class? You can take some refrence from https://github.com/sudip-bolakhe/taskapp/blob/master/taskapp/src/test/java/com/sudip/taskapp/TestTaskLogController.java – Sudip Bolakhe Mar 17 '19 at 17:53
  • Please [edit] your question to include the full exception message and the full stack trace of the exception. – Progman Mar 17 '19 at 17:56

1 Answers1

0

You need to put the TestService in the constructor of TestApiController, so you can inject it when creating the object in the test.

public class TestApiController {

private TestFeedService testService; // This is an interface 

@Autowired
public TestApiController(TestFeedService testService ) {
    this.testService = testService;
}

@RequestMapping(path = "/api/process-message", method = RequestMethod.POST)
public ResponseEntity<?> processMessage(@RequestBody TestForm2 testForm) {
  DataBO dataBO = convertBO(testForm);
  testService.sendMessage(dataBO, false);  // here I am getting testService = null and causing exception
  return ResponseEntity.ok().build();
} // Some more code

So your test would be like:

public class testApiControllerTest {
private MockMvc mockMvc;
private TesApiController testApiController;
private TestFeedService testService;

@Before
public void setup() {
  this.testService = mock(TestFeedService.class);
}

@Test
public void testProcessMessage() {
  this.testApiController = new TestApiController(this.testService);
  // More code
}}

Hope it helps