0

I am using MockMvc for the first type. What I am doing is testing a Restcontroller in SPringboot.

I have a Restcontroller like this,

    @RequestMapping("/address")
    public class AddressController {

    @RequestMapping("/createAddress")
        public Address craeteAddress(Address address)
        {
            Address add=addressService.createAdd(address);
            return add;
        }

@RequestMapping("/getAll")
    public List<Address> getAll()
    {
        return addressService.getAll();

    }




    }

Now my test class looks like this,

public class AddressControllerTest {

    AddressService addressService = mock(AddressService.class);

    @Autowired
    MockMvc mockMvc;

    private static final String ADDRESS_DTO_JSON =
            "{" +
            "\"id\":\"123\"," +
            "\"pacsId\":\"345\"," +
            "}";

    List<Object> object = new ArrayList<Object>();


        @Before
        public void setup() {


        }

    @Test       
    public void createAddressTest() throws Exception {

        //System.out.println("The Json Content :" + ADDRESS_DTO_JSON);
        this.mockMvc
        .perform(get("address/getAll"))
        .andExpect(status().isOk())
        .andReturn();       
    }

}

I am trying to call the 'address/getAll' method in the restcontroller. When i run the junit method it says 'Null Pointer Exception' near the .perform(get("address/getAll")) line.

I did some research but did not work out. Can someone please help ?

  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Sep 22 '18 at 15:03
  • may be not.. i am passing everything required to do so. – chandu15081990 Sep 22 '18 at 15:06

1 Answers1

0

I think you misconfigured your test class. Try something like this:

@RunWith(SpringRunner.class)
@WebMvcTest(AddressController.class)
public class AddressControllerTest {
  @Autowired
  MockMvc mockMvc;
}
akuma8
  • 4,160
  • 5
  • 46
  • 82