I get an error when trying to start my JUnit test. When I call the method I got the error "java. lang.AssertionError: Status expected:<201> but was:<400>".
What am I doing wrong ? URL is right, the program works correctly, but the test has not passed.
My Controller:
@RestController
@RequestMapping(value = "addresses", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class AddressController {
private final AddressService addressService;
@GetMapping("/{id}")
public AddressResponse getAddress(@PathVariable Integer id) {
return addressService.getAddress(id)
.map(AddressResponse::convertToResponse)
.orElse(null);
}
@GetMapping
public List<AddressResponse> 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("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Address deleteAddress(@PathVariable Integer id) {
return addressService.deleteAddress(id);
}
My Service:
@Service
@RequiredArgsConstructor
public class AddressService {
private final AddressRepository addressRepository;
public Optional<Address> getAddress(Integer id) {
return addressRepository.findById(id);
}
public List<Address> 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(() -> new RuntimeException("Cannot find address by id"));
}
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<Address> optional = addressRepository.findById(id);
if (optional.isPresent()) {
address = optional.get();
addressRepository.deleteById(id);
}
return address;
}
}
}
And My Test Class:
@ExtendWith(MockitoExtension.class)
class AddressControllerTest {
@Mock
private AddressService addressService;
private com.second_bank.entities.Address address;
private List<Address> addressList;
@InjectMocks
private AddressController addressController;
@Autowired
MockMvc mockMvc;
@BeforeEach
public void setup() {
com.second_bank.entities.Address address = new com.second_bank.entities.Address("City", "Street", "House");
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("/addresses")
.contentType(MediaType.APPLICATION_JSON)
.content(asJsonString(address)))
.andExpect(status().isCreated());
verify(addressService, times(1)).upsertAddress(any());
}
I create a method to convert objects to JSON
public static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
DTO Response:
@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;
}
CodePudding user response:
Not sure if this is the problem, but it seems that you are expecting an AddressRequest object in the body, but you are sending an Address object in your test. If the JSON conversion to the object is failing, it can return a 400 error.