title: Null Pointer Exception on MockMvc.perform on @Test class
body: <p>I am writing to write a unit test for my RestController (POST) and I am getting a NullPointerException on mvc.perform(...) line. </p>
  
  <p>Here's my RestController :</p>
  
  <pre><code>@RestController
  @EnableAutoConfiguration
  public class MyController {
  
  @Autowired
  private Service1 service;
  
  @Autowired
  RestTemplate restTemplate;
  
  @RequestMapping(value = "/logError", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
  @ResponseBody
  public ResponseEntity ErrorHandlor(@RequestBody JSONStructure jsonStructure) throws Exception{
  
  
      service.getDocument(jsonStructure.getID(), jsonStructure.getLog());
  
      return new ResponseEntity(HttpStatus.OK);
  
     }
  
  }
  </code></pre>
  
  <p>And here is my test class:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @ContextConfiguration(classes = {MyController.class, 
  Service1.class, AppConfig.class})
  @WebMvcTest(MyController.class)
  public class MyControllerTest {
  
  private MockMvc mockMvc;
  
  @MockBean
  private RestTemplate restTemplate;
  MyController service = new MyController();
  
  @Before
  public void setUp() {
      MockitoAnnotations.initMocks(this);
      this.mockMvc = MockMvcBuilders.standaloneSetup(service).build();
  }
  
  @Test
  public void testController() throws Exception{
  
      ObjectMapper mapper = new ObjectMapper();
  
      String url = "http://localhost:8080/logError";
  
      JSONStructure structure = new JSONStructure();
      structure.setNumber("num");
      structure.setID("id");
      structure.setLog("log");
  
      String json = mapper.writeValueAsString(structure)
      this.mockMvc.perform
      (MockMvcRequestBuilders.post("http://localhost:8080/logError")
      .contentType(MediaType.APPLICATION_JSON)
      .content(json))
      .andExpect(MockMvcResultMatchers.status().isCreated())
      .andReturn();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am getting a NPE on line containing this.mockMvc.perform(...).</p>
  
  <p>Can anyone point out what might the problem be?</p>