0

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);    
    }
Veda
  • 577
  • 1
  • 6
  • 22
  • 1
    Can you provide more information of how they fail when they are separate files? – user3127632 Oct 04 '18 at 22:27
  • Which test fails? Your 'ContractControllerTest' needs @WebMvcTest or something else. – Min Hyoung Hong Oct 05 '18 at 00:46
  • Error message when they are separate files org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.ContractControllerTest': Unsatisfied dependency expressed through field 'mockMvc'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} – Veda Oct 05 '18 at 13:22
  • @WebMvcTest can be used only in one controller #Min Hyoung Hong – Veda Oct 05 '18 at 13:24

0 Answers0