如何在运行测试(gradle / kotlin / springboot)之前运行应用程序

时间:2018-11-08 00:52:09

标签: gradle cucumber circleci

我目前正在尝试为cloudfoundery准备一个应用程序。此特定的CF实例仅支持CircleCI。我的应用程序有很多单元测试和一些黄瓜测试。我正在尝试使用CircleCI让它们通过befoure来进行推送。

CircleCI每次运行时,都会在“ gradle test”命令上失败。查看日志,单元测试通过,黄瓜测试失败。 我相信这是因为黄瓜测试要求应用程序正在运行。

这是黄瓜测试的堆栈跟踪示例:

java.net.ConnectException: Connection refused: connect

    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
    at sun.net.www.http.HttpClient.New(HttpClient.java:339)
    at sun.net.www.http.HttpClient.New(HttpClient.java:357)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:730)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:706)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:299)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:288)
    at cucumber.runtime.kotlin.test.StepDefinitions.getPage(StepDefinitions.kt:29)
    at ✽.I am on the main page(features/SearchWithFilters.feature:8)

这是我当前的config.yml:

version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:8-jdk

    working_directory: ~/repo

    environment:
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx3200m
      TERM: dumb

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "build.gradle" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: gradle dependencies

      - save_cache:
          paths:
            - ~/.gradle
          key: v1-dependencies-{{ checksum "build.gradle" }}

      - run: gradle clean assemble jar
      - run: java -jar build/libs/definitions-catalogue-1.0.jar &

      # run tests
      - run: gradle test

1 个答案:

答案 0 :(得分:0)

我使用dockerize解决了这个问题。

涉及三个步骤。

  1. 在后台运行应用程序
  2. 使用dockerize等待端口打开(这在应用程序侦听cretin端口时起作用。
  3. 运行测试。

执行此操作的代码如下:

      - run: java -jar definitions-catalogue-1.0.jar &

      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Run the application
          background: true
          command: gradle run
      - run:
          name: Wait for web service
          command: dockerize -wait http://localhost:5002/definitions -timeout 1m

      # run tests!
      - run: gradle featureTest