title: AssertionError: Status expected:<201> but was:<400> JUnit POST Method
body: <p>I get an error when trying to start my JUnit test. When I call the method I got the error &quot;java. lang.AssertionError: Status expected:&lt;201&gt; but was:&lt;400&gt;&quot;.</p>
  <p>What am I doing wrong ?
  URL is right, the program works correctly, but the test has not passed.</p>
  <p>My Controller:</p>
  <pre><code>@RestController
  @RequestMapping(value = &quot;addresses&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
  @RequiredArgsConstructor
  public class AddressController {
  
      private final AddressService addressService;
  
  
      @GetMapping(&quot;/{id}&quot;)
      public AddressResponse getAddress(@PathVariable Integer id) {
          return addressService.getAddress(id)
                  .map(AddressResponse::convertToResponse)
                  .orElse(null);
      }
  
      @GetMapping
      public List&lt;AddressResponse&gt; getAllAddresses() {
          return addressService.getAllAddresses()
                  .stream()
                  .map(AddressResponse::convertToResponse)
                  .collect(Collectors.toList());
      }
  
      @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
      public AddressResponse createAddress(@RequestBody AddressRequest request) {
          return AddressResponse.convertToResponse(addressService.upsertAddress(request));
      }
  
      @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
      public AddressResponse updateAddress(@RequestBody AddressRequest request) {
          return AddressResponse.convertToResponse(addressService.upsertAddress(request));
      }
  
      @DeleteMapping(&quot;/{id}&quot;)
      @ResponseStatus(HttpStatus.NO_CONTENT)
      public Address deleteAddress(@PathVariable Integer id) {
          return addressService.deleteAddress(id);
      }
  </code></pre>
  <p>My Service:</p>
  <pre><code>@Service
  @RequiredArgsConstructor
  public class AddressService {
  
      private final AddressRepository addressRepository;
  
  
      public Optional&lt;Address&gt; getAddress(Integer id) {
          return addressRepository.findById(id);
      }
  
      public List&lt;Address&gt; getAllAddresses() {
          return addressRepository.findAll();
      }
  
  
      public Address upsertAddress(@NonNull AddressRequest addressRequest) {
  
  
          Address entity;
  
          if (addressRequest.getId() == null) {
              entity = new Address();
          } else {
              entity = addressRepository.findById(addressRequest.getId())
                      .orElseThrow(() -&gt; new RuntimeException(&quot;Cannot find address by id&quot;));
          }
  
          entity.setCity(addressRequest.getCity());
          entity.setStreet(addressRequest.getStreet());
          entity.setHouse(addressRequest.getHouse());
  
          entity = addressRepository.save(entity);
  
          return entity;
      }
  
      public Address deleteAddress(Integer id) {
  
          Address address = null;
          Optional&lt;Address&gt; optional = addressRepository.findById(id);
  
          if (optional.isPresent()) {
              address = optional.get();
              addressRepository.deleteById(id);
          }
          return address;
      }
  
  }
  </code></pre>
  <p>}</p>
  <p>And My Test Class:</p>
  <pre><code>@ExtendWith(MockitoExtension.class)
  class AddressControllerTest {
  
      @Mock
      private AddressService addressService;
      private com.second_bank.entities.Address address;
      private List&lt;Address&gt; addressList;
  
      @InjectMocks
      private AddressController addressController;
  
      @Autowired
      MockMvc mockMvc;
  
      @BeforeEach
      public void setup() {
          com.second_bank.entities.Address address = new com.second_bank.entities.Address(&quot;City&quot;, &quot;Street&quot;, &quot;House&quot;);
          mockMvc = MockMvcBuilders.standaloneSetup(addressController).build();
      }
  
      @AfterEach
      public void tearDown() {
          address = null;
          addressList = null;
      }
  
   
     
  
      @Test
      void createAddress() throws Exception {
  
          when(addressService.upsertAddress(any())).thenReturn(address);
  
          mockMvc.perform(post(&quot;/addresses&quot;)
                  .contentType(MediaType.APPLICATION_JSON)
                  .content(asJsonString(address)))
                  .andExpect(status().isCreated());
          verify(addressService, times(1)).upsertAddress(any());
      }
  </code></pre>
  <p>I create a method to convert objects to JSON</p>
  <pre><code>public static String asJsonString(final Object obj) {
          try {
              return new ObjectMapper().writeValueAsString(obj);
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  </code></pre>
  <p>DTO Response:</p>
  <pre><code>@Data
  @JsonIgnoreProperties(ignoreUnknown = true)
  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  public class AddressResponse {
  
      private Integer id;
      private String city;
      private String street;
      private String house;
  
      public static AddressResponse convertToResponse(Address address){
  
          return new AddressResponse(
                  address.getId(),
                  address.getCity(),
                  address.getStreet(),
                  address.getHouse()
          );
      }
  }
  
  DTO Request:
  
  @Data
  @JsonIgnoreProperties(ignoreUnknown = true)
  public class AddressRequest {
  
      private Integer Id;
      private String city;
      private String street;
      private String house;
  
  
  }
  </code></pre>