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