title: Proper way to test POST method in Spring MVC Controller
body: <p>I've got simple app based on SpringMVC and Hibernate, trying to test a <code>POST</code> method which saves a Customer <code>Customer.class</code> to the database. Don't want to test database yet, only simple unit testing. Got the test green, but feel is still not enough, how can I refactor it to make it as it supposed to be?</p>
  
  <p>CustomerController.class:</p>
  
  <pre><code>@Controller
  @RequestMapping("/customer")
  public class CustomerController {
  
    @Autowired
      private CustomerService customerService;
  
    @PostMapping("/saveCustomer")
      public String saveCustomer(@ModelAttribute("customer") Customer theCustomer) {
          customerService.saveCustomer(theCustomer);
  
          return "redirect:/customer/list";
      }}
  </code></pre>
  
  <p>CustomerServiceImpl.class:</p>
  
  <pre><code>@Service
  public class CustomerServiceImpl implements CustomerService {
    @Override
    @Transactional
      public void saveCustomer(Customer theCustomer) {
          customerDAO.saveCustomer(theCustomer);
      }
  }
  </code></pre>
  
  <p>CustomerDAOImpl.class:</p>
  
  <pre><code>@Repository
  public class CustomerDAOImpl implements CustomerDAO {
    @Override
      public void saveCustomer(Customer theCustomer) {
  
          Session currentSession = sessionFactory.getCurrentSession();
  
          currentSession.saveOrUpdate(theCustomer);
  
      }
  }
  </code></pre>
  
  <p>CustomerControllerTest.class:</p>
  
  <pre><code>public class CustomerControllerTest {
  
  
  @InjectMocks
  CustomerController controller;
  
  @Mock
  CustomerService mockCustomerService;
  
  @Mock
  View mockView;
  
  
  MockMvc mockMvc;
  
  @Before
  public void setUp() throws Exception {
  
      MockitoAnnotations.initMocks(this);
  
      mockMvc = MockMvcBuilders.standaloneSetup(controller)
              .setSingleView(mockView)
              .build();
  }
       @Test
          public void testSaveCustomer() throws Exception {
  
          mockMvc.perform(post("/customer/saveCustomer"))
                  .andExpect(status().isOk())
                  .andExpect(view().name("redirect:/customer/list"));
  
      }}
  </code></pre>