title: How to write unit test for controller in spring boot
body: <p>I am new to Unit testing and TDD. I want to apply unit testing for my controller and service class which i have written in spring boot.</p>
  
  <p>I have implemented test class using a tutorial. However, I could not implemented it successfully. I have included my current code.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/api")
  public class MyController {
      private static final Logger LOGGER = LoggerFactory.getLogger(AdminController.class);
      @Autowired
      MyService myService;
  
      @PostMapping("/create")
      public ResponseEntity&lt;?&gt; createUser(@RequestHeader("Authorization") String token, 
          @RequestBody User user){
          ResponseDTO finalResponse = new ResponseDTO();
          try {
              ResponseEntity&lt;?&gt; entity = myService.create(token, user);             
              finalResponse.setMessageCode(entity.getStatusCode());
              finalResponse.setMessage("Success");
              finalResponse.setError(false);
              ResponseEntity&lt;ResponseDTO&gt; finalEntity = ResponseEntity.ok().body(finalResponse);
          return finalEntity;
          } catch (Exception e) {      
              finalResponse.setMessageCode(HttpStatus.EXPECTATION_FAILED);
              finalResponse.setMessage(e.getMessage());
              finalResponse.setError(true);
              ResponseEntity&lt;ResponseDTO&gt; finalEntity = 
              ResponseEntity.ok().body(finalResponse);
              return finalEntity;
      }
  }
  </code></pre>
  
  <p><strong>ResponseDTO</strong></p>
  
  <pre><code>public class ResponseDTO {
      private HttpStatus messageCode;
      private String message;
      private String messageDetail;
      private Object body;
      private boolean error;
  
      //setters and getters
  }
  </code></pre>
  
  <p><strong>Current Test Class</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  public class MyControllerTest {
      private MockMvc mockMvc;
  
      @InjectMocks
      private MyController myController;
  
      @Before
      public void setUp() throws Exception {
      mockMvc = MockMvcBuilders.standaloneSetup(myController).build();
      }
  
      @Test
      public void testCreateUser() throws Exception {
          mockMvc.perform(post("/api/create")
              .accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isCreated())
              .andExpect(jsonPath("$.*", Matchers.hasSize(1)));
      }
  
  }
  </code></pre>
  
  <p>When i run test class i am getting <code>WARN Resolved [org.springframework.web.bind.MissingRequestHeaderException: Missing request header 'Authorization' for method parameter of type String]</code></p>
  
  <p>What am i doing wrong here? Any help would be grateful.</p>