I am using JUnit and Mockito to write test cases for Spring Boot Application. I have multiple controllers(For eg: ContractController and CountryCOntroller). When I write test cases for both of them in a single file , test will pass but if I write ContractController test cases in one file and the other controller test cases in second file , test cases fail. Can you please let me know how to write in different files?
Contract Controller TEst
package com.example.demo;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.example.demo.controllers.ContractController;
import com.example.demo.controllers.CountryController;
import com.example.demo.entities.Contract;
import com.example.demo.entities.Country;
import com.example.demo.repositories.ContractRepository;
import com.example.demo.repositories.CountryRepository;
@RunWith(SpringRunner.class)
public class ContractControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ContractRepository contractRepository;
@SuppressWarnings("unchecked")
@Test
public void testGetContract() throws Exception {
System.out.println("contract testing");
Contract contract = new Contract();
contract.setContractTypeId(1);
contract.setContractType("Calibration");
Mockito.when(this.contractRepository.findAll()).thenReturn((Collections.singletonList(contract)));
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/api/contractType").accept(MediaType.APPLICATION_JSON_UTF8);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
System.out.println("result is"+result.getResponse().getContentAsString());
String expected = "[{id:1,contractType:Calibration}]";
JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);
}
}
COuntry COntroller Test
package com.example.demo;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.example.demo.controllers.CountryController;
import com.example.demo.entities.Contract;
import com.example.demo.entities.Country;
import com.example.demo.repositories.ContractRepository;
import com.example.demo.repositories.CountryRepository;
@RunWith(SpringRunner.class)
@WebMvcTest(value = CountryController.class)
public class CountryControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CountryRepository countryRepository;
@MockBean
private ContractRepository contractRepository;
@SuppressWarnings("unchecked")
@Test
public void testGetCountries() throws Exception {
System.out.println("mockito testing");
Country country = new Country();
country.setId(1);
country.setCountryName("Afghanistan");
country.setShortName("AF");
Mockito.when(this.countryRepository.findAll()).thenReturn((Collections.singletonList(country)));
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/api/countries").accept(MediaType.APPLICATION_JSON_UTF8);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
System.out.println("result is"+result.getResponse().getContentAsString());
String expected = "[{id:1,shortName:AF,countryName:Afghanistan}]";
JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);
}