-
-
Save wkorando/2e05a94560cd586b39392f410f6c667e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ibm.developer.produce; | |
import static org.mockito.Mockito.when; | |
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | |
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; | |
import java.util.Arrays; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.rules.TestName; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.boot.test.mock.mockito.MockBean; | |
import org.springframework.restdocs.JUnitRestDocumentation; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |
import io.restassured.module.mockmvc.RestAssuredMockMvc; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) | |
public class ContractsBaseClass { | |
@Rule | |
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); | |
@Rule | |
public TestName testName = new TestName(); | |
@Autowired | |
private ProduceController controller; | |
@MockBean | |
private ProduceRepo repo; | |
@MockBean | |
private ProduceService service; | |
@Before | |
public void before() throws Throwable { | |
when(repo.findAll()).thenReturn( | |
Arrays.asList(new Produce(1, "Apple", "Granny Smith", 100), | |
new Produce(2, "Apple", "Gala", 50),// | |
new Produce(3, "Corn", "Sweet", 1000), // | |
new Produce(4, "Pineapple", "", 300))); | |
when(service.findProduceByName("Apple")).thenReturn( | |
Arrays.asList(new Produce(1, "Apple", "Granny Smith", 100), | |
new Produce(2, "Apple", "Gala", 50))); | |
when(service.findProduceByName("+")).thenThrow(new ClientException("Produce name must be alpha numeric!")); | |
when(service.addNewProduce(new Produce(0, "Kiwi", "", 75))).thenReturn(new Produce(10, "Kiwi", "", 75)); | |
when(service.addNewProduce(new Produce(0, "", "", 75))).thenThrow(new ClientException("Missing required value!")); | |
RestAssuredMockMvc.standaloneSetup(MockMvcBuilders | |
.standaloneSetup(controller) | |
.apply(documentationConfiguration(this.restDocumentation)) | |
.alwaysDo(document(getClass().getSimpleName() + "_" + testName.getMethodName()))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment