contract-testing-demo

pact and java contract testing with micro-commits

git clone https://9o.is/git/contract-testing-demo.git

ZooServicePactTest.java

(2535B)


      1 package app;
      2 
      3 import app.alligator.Alligator;
      4 import app.alligator.AlligatorController;
      5 import app.alligator.AlligatorRepository;
      6 import au.com.dius.pact.provider.junit.Provider;
      7 import au.com.dius.pact.provider.junit.RestPactRunner;
      8 import au.com.dius.pact.provider.junit.State;
      9 import au.com.dius.pact.provider.junit.StateChangeAction;
     10 import au.com.dius.pact.provider.junit.loader.PactFolder;
     11 import au.com.dius.pact.provider.junit.target.TestTarget;
     12 import au.com.dius.pact.provider.spring.target.MockMvcTarget;
     13 import org.junit.Before;
     14 import org.junit.runner.RunWith;
     15 import org.mockito.InjectMocks;
     16 import org.mockito.Mock;
     17 import org.mockito.MockitoAnnotations;
     18 
     19 import static org.mockito.Mockito.verify;
     20 import static org.mockito.Mockito.when;
     21 
     22 @RunWith(RestPactRunner.class)
     23 @Provider("Animal Service")
     24 @PactFolder("../pacts")
     25 public class ZooServicePactTest {
     26 
     27     @TestTarget
     28     public final MockMvcTarget target = new MockMvcTarget();
     29 
     30     @InjectMocks
     31     private AlligatorController alligatorController;
     32 
     33     @Mock
     34     private AlligatorRepository alligatorRepository;
     35 
     36     @State("there exists an alligator named Mary")
     37     public void alligatorMaryExists() {
     38         when(alligatorRepository.getByName("Mary"))
     39                 .thenReturn(new Alligator("Mary"));
     40     }
     41 
     42     @State(
     43         value = "there exists an alligator named Mary",
     44         action = StateChangeAction.TEARDOWN
     45     )
     46     public void alligatorMaryExistsVerification() {
     47         verify(alligatorRepository).getByName("Mary");
     48     }
     49 
     50     @State("there does not exist an alligator named Mary")
     51     public void alligatorMaryDoesNotExist() {
     52         when(alligatorRepository.getByName("Mary"))
     53                 .thenReturn(null);
     54     }
     55 
     56     @State(
     57         value = "there does not exist an alligator named Mary",
     58         action = StateChangeAction.TEARDOWN
     59     )
     60     public void alligatorMaryDoesNotExistVerification() {
     61         verify(alligatorRepository).getByName("Mary");
     62     }
     63 
     64     @State("the service is unavailable")
     65     public void animalServiceIsUnavailable() {
     66         when(alligatorRepository.getByName("Mary"))
     67                 .thenThrow(RuntimeException.class);
     68     }
     69 
     70     @State(
     71         value = "the service is unavailable",
     72         action = StateChangeAction.TEARDOWN
     73     )
     74     public void animalServiceIsUnavailableVerification() {
     75         verify(alligatorRepository).getByName("Mary");
     76     }
     77 
     78     @Before
     79     public void before() {
     80         MockitoAnnotations.initMocks(this);
     81         target.setControllers(alligatorController);
     82     }
     83 }