如何在多个文件中分别为多个控制器编写Spring Boot测试用例

时间:2018-10-04 22:03:55

标签: spring spring-boot mockito junit5 spring-boot-test

我正在使用JUnit和Mockito编写Spring Boot Application的测试用例。我有多个控制器(例如:ContractController和CountryCOntroller)。当我在一个文件中同时编写两个测试用例时,测试将通过,但是如果我在一个文件中写入ContractController测试用例,而在第二个文件中写入另一个控制器测试用例,则测试用例将失败。 你能让我知道如何写不同的文件吗?

合同负责人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测试

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);    
    }

0 个答案:

没有答案