com.recursivechaos.clearent.controller.SaleControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.recursivechaos.clearent.controller.SaleControllerTest.java

Source

/**
 * Created by Andrew Bell 2/1/2016
 * www.recursivechaos.com
 * andrew@recursivechaos.com
 * Licensed under MIT License 2016. See license.txt for details.
 */

package com.recursivechaos.clearent.controller;

import com.recursivechaos.clearent.TestUtil;
import com.recursivechaos.clearent.domain.Sale;
import com.recursivechaos.clearent.service.ResponseService;
import com.recursivechaos.clearent.service.SaleService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.net.URI;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

public class SaleControllerTest {

    @InjectMocks
    private SaleController saleController;

    @Mock
    SaleService saleService;

    @Mock
    ResponseService responseService;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testPostSale() throws Exception {
        Sale saleRequest = TestUtil.createSaleRequest();

        when(saleService.createSale(saleRequest)).thenReturn(saleRequest);
        URI uri = new URI("/sales/1");
        when(responseService.getLocationUri(saleRequest)).thenReturn(uri);

        ResponseEntity<Void> responseEntity = saleController.postSale(saleRequest);

        assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
        assertEquals("/sales/1", responseEntity.getHeaders().getLocation().getPath());
    }

}