(Spring Boot)scala集成测试失败,无法确定Swagger2集成后资源类路径资源的真实路径

时间:2019-02-09 12:44:05

标签: java spring spring-boot swagger springfox

以下是我的配置

<spring-boot.version>1.5.13.RELEASE</spring-boot.version>
<spring-data.version>1.7.11.RELEASE</spring-data.version>
<spring-cloud.version>1.2.0.RELEASE</spring-cloud.version>
<springfox.version>2.9.2</springfox.version>
<scala.major.version>2</scala.major.version>
<scala.minor.version>11</scala.minor.version>
<scala.maintenance.version>7</scala.maintenance.version>
Java - 1.8

Swagger配置如下,

/**
 * Configuration class for Swagger API documentation
 */
@EnableSwagger2
@Configuration
public class SwaggerConfiguration
{
    @Value("${swagger.service.title}")
    private String serviceTitle;

    @Value("${swagger.service.description}")
    private String serviceDescription;

    @Value("${swagger.service.termsPath}")
    private String serviceTermsPath;

    @Value("${swagger.service.email}")
    private String serviceEmail;

    @Value("${swagger.service.licenceType}")
    private String serviceLicenceType;

    @Value("${swagger.service.licencePath}")
    private String serviceLicencePath;


    @Bean
    @SuppressWarnings("javadoc")
    public Docket swaggerSpringMvcPlugin()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(paths())
                .build()
                .pathMapping("/")
                .protocols(protocols())
                .apiInfo(apiInfo());
    }

    private Set<String> protocols()
    {
        return new HashSet<>(Arrays.asList("https", "http"));
    }

    private Predicate<String> paths()
    {
        return or(
                regex("/rest/.*?"));
    }

    private ApiInfo apiInfo()
    {

        return new ApiInfoBuilder()
                .title(serviceTitle)
                .description(serviceDescription)
                .termsOfServiceUrl(serviceTermsPath)
                .contact(new Contact("", "", this.serviceEmail))
                .license(serviceLicenceType)
                .licenseUrl(serviceLicencePath)
                .version("0.0.1")
                .extensions(Collections.emptyList())
                .build();
    }
}

我的scala集成测试如下,

@RunWith(classOf[SpringRunner])
@SpringBootTest(classes = Array(classOf[Starter]), properties = Array("server.port:0", "spring.profiles.active=dev"))
class DataVariantIntgSpec extends AcceptanceSpec with EasyMockSugar with PropertyChecks {

  // In order to test the exception and error resolving is globally enabled we need to start
  // the service and ensure everything is hooked up correctly
  @Autowired
  val webApplicationContext: WebApplicationContext = null

  @Value("${local.server.port}")
  val port = ""

  new TestContextManager(this.getClass).prepareTestInstance(this)

  feature("The data variant parameter") {
    scenario("A method is called without a data variant") {
      val mockMvc: MockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()

      val result = mockMvc
        .perform(MockMvcRequestBuilders.get("/rest/v2/state/678"))
        .andExpect(MockMvcResultMatchers.status.isOk)
        .andReturn()

      result.getResponse.getContentAsString should include("\"id\":678")

    }
    scenario("A method is called with a known data variant") {
      val mockMvc: MockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()

      val result = mockMvc
        .perform(MockMvcRequestBuilders.get("/rest/v2/state/678?dataVariant=test"))
        .andExpect(MockMvcResultMatchers.status.isOk)
        .andReturn()

      result.getResponse.getContentAsString should include("\"state\":678")
      result.getResponse.getContentAsString should include("\"actualDataVariant\":\"test\"")
    }
  }
}

当我尝试使用此应用时,出现以下编译错误

2019-02-08 15:15:39,603 [ScalaTest-main] org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver INFO Detected ResponseBodyAdvice implementation in org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration$ActuatorEndpointLinksAdvice
2019-02-08 15:15:39,894 [ScalaTest-main] org.springframework.boot.test.mock.web.SpringBootMockServletContext WARN Couldn't determine real path of resource class path resource [META-INF/resources/]
java.io.FileNotFoundException: class path resource [META-INF/resources/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/pmurugesan/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.jar!/META-INF/resources/
    at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53)
    at org.springframework.mock.web.MockServletContext.getRealPath(MockServletContext.java:458)
    at org.springframework.web.util.WebUtils.getRealPath(WebUtils.java:272)
    at org.springframework.web.context.support.ServletContextResource.getFile(ServletContextResource.java:176)
    at org.springframework.beans.propertyeditors.FileEditor.setAsText(FileEditor.java:103)
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:470)
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:443)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:200)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:588)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:615)
    at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:462)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:290)
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)
    at 

我提到了这个thread,但这并没有帮助我。请让我知道如何在运行集成测试时使用招摇工具解决问题。

0 个答案:

没有答案