test put method failed using MockMVC in Spring rest in Spring boot app
<p>I'm trying to test the put method of my rest controller class using MockMVC  in Spring rest in Spring boot app. the test always fails giving me this error</p>
<p><strong>java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;400&gt;</strong></p>
<p>below are the rest controller class</p>
<pre><code>package com.store.rest.controller;

import java.time.LocalDate;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.store.domain.Product;
import com.store.exception.ProductNotFoundException;
import com.store.repository.ProductRepository;
import com.store.rest.representation.ProductCollectionRepresentation;


@RestController
@Transactional
public class ProductController {

@Autowired
@Qualifier(&quot;productRepository&quot;)
private ProductRepository productRepository;

@RequestMapping(value = &quot;/product/id/{id}&quot;, method = RequestMethod.GET)
public Product getProductById(@PathVariable(&quot;id&quot;) Long productId)
        throws ProductNotFoundException {
    Product product = productRepository.findOne(productId);
    if (product == null) {
        throw new ProductNotFoundException(&quot;the product id &quot; + productId
                + &quot; is not existed&quot;);
    }
    return product;
}

@RequestMapping(value = &quot;/product/name/{name}&quot;, method = RequestMethod.GET)
public Product getProductByName(@PathVariable(&quot;name&quot;) String productName)
        throws ProductNotFoundException {

    Product product = productRepository.findByProductName(productName);
    if (product == null) {
        throw new ProductNotFoundException(&quot;the product name &quot;
                + productName + &quot; is not existed&quot;);
    }
    return product;
}

@RequestMapping(value = &quot;/product/sku/{sku}&quot;, method = RequestMethod.GET)
public Product getProductBySKU(@PathVariable(&quot;sku&quot;) String productSKU)
        throws ProductNotFoundException {

    Product product = productRepository.findByProductSku(productSKU);
    if (product == null) {
        throw new ProductNotFoundException(&quot;the product Sku&quot; + productSKU
                + &quot; is not existed&quot;);
    }
    return product;
}

@RequestMapping(value = &quot;/products&quot;, method = RequestMethod.GET)
public ProductCollectionRepresentation getProducts(
        @RequestParam(required = false) Integer first,
        @RequestParam(required = false) Integer last) {

    List&lt;Product&gt; products = productRepository.findAll();

    if (products == null) {
        throw new ProductNotFoundException(
                &quot;no Products are available in the store&quot;);
    }

    if (first != null &amp;&amp; (last != null &amp;&amp; last &lt; products.size())) {
        return new ProductCollectionRepresentation(products.subList(
                first - 1, last));
    } else if (first != null &amp;&amp; (last == null || last &gt; products.size())) {
        return new ProductCollectionRepresentation(products.subList(
                first - 1, products.size()));
    }

    return new ProductCollectionRepresentation(products);
}

@RequestMapping(value = &quot;/product/id/{id}&quot;, method = RequestMethod.PUT)
public Product updateProductById(@RequestBody Product product,
        @PathVariable(&quot;id&quot;) long productId) {

    Product foundProduct = productRepository.findOne(productId);
    if (foundProduct == null) {
        throw new ProductNotFoundException(&quot;the product Id&quot; + productId
                + &quot; is not existed to update it&quot;);
    }
    
    String productSku = product.getProductSku() != null?product.getProductSku():foundProduct.getProductSku();
    String productName = product.getProductName() != null?product.getProductName():foundProduct.getProductName();
    
    Product UpdatedProduct = new Product(foundProduct.getId(),
            productSku, productName,
            LocalDate.now());
    
    UpdatedProduct = productRepository.saveAndFlush(UpdatedProduct);
    
    return UpdatedProduct;
}

}
</code></pre>
<p>my test is</p>
<pre><code>  @RunWith(SpringRunner.class)
  @WebAppConfiguration
  public class ProductControllerTest  {

    private MediaType contentType = new MediaType(          MediaType.APPLICATION_JSON.getType(),
        MediaType.APPLICATION_JSON.getSubtype(),
        Charset.forName(&quot;utf8&quot;));
 
@InjectMocks
private ProductController productController;

@Mock
private ProductRepository productRepository;

private MockMvc mockMvc;


@Before
public void setUp() throws Exception {
    
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(productController)
        .setControllerAdvice(new RestErrorHandlerAdvice()).build();
    
}



@Test
public void testUpdateProductByIdExisted() throws Exception {
    
    Product product = new Product(1L,&quot;SKU_A&quot;, &quot;ProductA&quot;, LocalDate.of(2016, 8, 16));
    
    Product updatedProduct = new Product(&quot;SKU_A&quot;, &quot;Product_A1&quot;);
    
    when(productRepository.findOne(1L)).thenReturn(product);
     
     this.mockMvc.perform(
             MockMvcRequestBuilders.put(&quot;/product/id/{id}&quot;,1L)
            .contentType(contentType)
            .accept(contentType)
            .content(asJsonString(updatedProduct)))
            .andExpect(status().isOk())
            .andExpect(jsonPath(&quot;productSku&quot;, is(&quot;SKU_A&quot;)))
            .andExpect(jsonPath(&quot;productName&quot;, is(&quot;Product_A1&quot;)));
     
    verify(productRepository, times(1)).saveAndFlush(product);
    
}



public static String asJsonString(final Object obj) {
    try {
        return new ObjectMapper().writeValueAsString(obj);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
</code></pre>
<p>}</p>
<p>the domain class product is</p>
<pre><code>@Entity
@Table(name=&quot;product&quot;)
@XmlRootElement
public class Product implements Serializable{

private static final long serialVersionUID = 1L;
    
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = &quot;product_id&quot;)
private long productId;

@Column(name = &quot;product_sku&quot;, nullable=false, unique=true)
private String productSku;

@Column(name = &quot;product_name&quot;,nullable=false, unique=true)
private String productName;

@Column(name = &quot;product_created&quot;)
@Convert(converter = LocalDateConverter.class)
private LocalDate productCreated;


@Column(name = &quot;product_lastupdate&quot;)
@Convert(converter = LocalDateConverter.class)
private LocalDate productLastUpdated;

public Product(){
    
}

public Product(String sku,String productName){
    this.productSku = sku;
    this.productName = productName;
    this.productCreated = LocalDate.now();
    this.productLastUpdated = LocalDate.now();
}

public Product(String sku,String productName, LocalDate createdDate){
    this.productSku = sku;
    this.productName = productName;
    this.productCreated = createdDate;
    this.productLastUpdated = createdDate;
}

public Product(Long id,String sku,String productName, LocalDate createdDate){
    this.productId = id;
    this.productSku = sku;
    this.productName = productName;
    this.productCreated = createdDate;
    this.productLastUpdated = createdDate;
}

public long getId() {
    return productId;
}

public String getProductSku() {
    return productSku;
}

public void setProductSku(String productSku) {
    this.productSku = productSku;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public LocalDate getProductCreated() {
    return productCreated;
}

public void setProductCreated(LocalDate productCreated) {
    this.productCreated = productCreated;
}

public LocalDate getProductLastUpdated() {
    return productLastUpdated;
}

public void setProductLastUpdated(LocalDate productLastUpdated) {
    this.productLastUpdated = productLastUpdated;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((productName == null) ? 0 : productName.hashCode());
    result = prime * result
            + ((productSku == null) ? 0 : productSku.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Product other = (Product) obj;
    if (productName == null) {
        if (other.productName != null)
            return false;
    } else if (!productName.equals(other.productName))
        return false;
    if (productSku == null) {
        if (other.productSku != null)
            return false;
    } else if (!productSku.equals(other.productSku))
        return false;
    return true;
}

@Override
public String toString() {
    return &quot;Product [productId=&quot; + productId + &quot;, productSku=&quot; + productSku
            + &quot;, productName=&quot; + productName + &quot;, productCreated=&quot;
            + productCreated + &quot;, productLastUpdated=&quot; + productLastUpdated
            + &quot;]&quot;;
}
}
</code></pre>
<p>any idea why I get 400 status instead of 200 in the test?</p>