0

org.springframework.web.util.NestedServletException: Request processing failed

Get this exception error when try to execute Junit Test for restApi method to DeleteById

the problem that in Controller implements when i change return type of method from DeviceTO to device it works but i need to be returned as DeviceTO (DTO) so i faced this issue :

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

Please help me

this is the code for test class:


    @RunWith(MockitoJUnitRunner.class)
    @EnableWebMvc
    public class DeviceControllerFacadeTest {
    
    @Mock
    DeviceServiceImpl deviceServiceMock;
    @MockBean
    DeviceControllerFacadeImpl deviceControllerFacade = new DeviceControllerFacadeImpl();
    
    MockMvc mockMvc;


    /**
     * 
     */
    @Before
    public void setUp() {

        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(deviceControllerFacade)
                .build();

    }

    /**
     * @throws java.lang.Exception
     */

    @After
    public void tearDown()
            throws Exception
    {
    }

    
    /**
     * @throws Exception
     */
    @Test
    public void DeleteDeviceById()
            throws Exception
    {
            mockMvc.perform(MockMvcRequestBuilders
                    .delete("/device/delete?id=336").accept(MediaType.APPLICATION_JSON)
                              .contentType(MediaType.APPLICATION_JSON))
                              .andDo(MockMvcResultHandlers.print());
     
    }
    
    

and this is the controller interface :

    @DeleteMapping("/delete")
    DeviceTO deleteDevice(@RequestParam("id") long id);

and this is for Controller implements class:

     @Override
     public DeviceTO  deleteDevice(long id)
    {
      

        try {

            begin();

            Device device = deviceService.getEntity(id);

            //deviceService.delete(device);
            DeviceTO deviceTO = new DeviceTO();
            deviceTO.mergeEntityToTransferObject(device);
            commit();
            return deviceTO;

        } catch (

        final Exception exception) {
            LOG.error(exception.getMessage(), exception);
            throw  exception;

        } finally {
            cleanupTransaction();

        }
    }

Thank you for helping

I need this issue of exception will never appear again and the Test of method deleteById worked and return the device are deleted and the return type is DeviceTO not Device.

Thank you for helping.

Keyvan Soleimani
  • 606
  • 2
  • 4
  • 16
MR Int
  • 1
  • 2
  • Please read: [Why is β€œCan someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) --- "*i expect a solution to be working*" - I suggest [edit]ing and rephrasing this part of the post. Having expectations on a Q&A site where the community provides help for free is an attitude that is not well regarded around here. – Turing85 Dec 29 '22 at 14:16
  • i did edit it as you asked for – MR Int Dec 29 '22 at 14:33

0 Answers0