带有Maven Spring项目的MockMvc给了我=>通过字段'mockMvc'表达的不满意依赖性

时间:2018-08-10 00:11:53

标签: java spring spring-mvc spring-webflux

package com.activemeasure.validationservice.integration;

import com.activemeasure.validationservice.dto.RequestPostalAddressDTO;
import com.google.gson.Gson;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class AB {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void getEmailAliasValidation_whenItIsValidUSAAddress_shouldStandardizingIt() throws Exception {
        RequestPostalAddressDTO requestPostalAddressDTO = new RequestPostalAddressDTO();
        requestPostalAddressDTO.setGeoStreetAddress1("550 N Brand, 1850");
        requestPostalAddressDTO.setGeoPostalCode("91203");

        Gson gson = new Gson();
        String json = gson.toJson(requestPostalAddressDTO);

        this.mockMvc.perform(post("/v1/postaladdress/us")
                .content(json)
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}

通过这种设置,我不断通过字段'mockMvc'表示不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“ org.springframework.test.web.servlet.MockMvc”的合格Bean:至少应有1个可以作为自动装配候选的bean。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

我的控制器是这样的:

package com.activemeasure.validationservice.controller;

import com.activemeasure.validationservice.dto.ResponseAddressStandardizationDTO;
import com.activemeasure.validationservice.dto.RequestPostalAddressDTO;
import com.activemeasure.validationservice.exception.NotAValidEndpointExeption;
import com.activemeasure.validationservice.service.AddressStandardizationService;
import com.activemeasure.validationservice.transformers.AddressStandardizationTransformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "/v1")
public class AddressStandardizationController {

    private final AddressStandardizationService addressStandardizationService;
    private final AddressStandardizationTransformers addressStandardizationTransformers;

    @Autowired
    public AddressStandardizationController(AddressStandardizationService addressStandardizationService,
                                            AddressStandardizationTransformers addressStandardizationTransformers) {
        this.addressStandardizationService = addressStandardizationService;
        this.addressStandardizationTransformers = addressStandardizationTransformers;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/postaladdress/{countryCode}")
    public Mono<ResponseAddressStandardizationDTO> getStandardizedAddress(
            @RequestBody @Valid RequestPostalAddressDTO requestPostalAddressDTO, @PathVariable("countryCode") String countryCode) {
        if (countryCode.length() != 2) {
            throw new NotAValidEndpointExeption("Country code must be two letters");
        }
        return addressStandardizationService.standardized(addressStandardizationTransformers.transformForUSA(requestPostalAddressDTO), countryCode);

    }

}

顺便说一句,我在Maven中使用spring Webflux。我不知道还能做些什么,也找不到任何可以帮助我的在线方法。我尝试了很多在线说的东西,但是没有用。

0 个答案:

没有答案