【问题标题】:error while running integration test with spring boot and testng使用 spring boot 和 testng 运行集成测试时出错
【发布时间】:2016-11-05 14:47:58
【问题描述】:

我们正在尝试使用 testng 运行集成测试,springboot.app 运行良好,我们可以使用 eclipse 运行独立测试。但是当我们使用 maven 命令行运行测试用例时。我得到以下异常

org.apache.coyote.AbstractProtocol start
SEVERE: Failed to start end point associated with ProtocolHandler ["http-nio-8080"]
java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:433)
        at sun.nio.ch.Net.bind(Net.java:425)

如果我们尝试加载 context.xml 以运行测试用例,那么每个测试用例都在尝试加载自己的上下文,并且测试用例失败并出现未找到主机的异常。

基本测试用例类

//@ContextConfiguration(locations={"classpath:objectModelContext.xml"})
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseITCase extends com.test.common.BaseITCase {

    protected static final String baseUrl = "http://localhost:" +  port + "/test/api/";

    protected String getAdminToken(String username, String password, String deviceId) throws Exception {
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>om.test</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.test.App</start-class>
    <tomcat.version>7.0.65</tomcat.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections-maven</artifactId>
        <version>0.9.9-RC2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-velocity</artifactId>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <!-- only dependency needed -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.googlecode.plist</groupId>
        <artifactId>dd-plist</artifactId>
        <version>1.8</version>
        <!-- corresponds to r108 -->
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <!-- Test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.13</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-web</artifactId>
        <version>1.2.4</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <maxAttempts>600</maxAttempts>
                <fork>true</fork>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <!--$NO-MVN-MAN-VER$-->
            <executions>
                <execution>
                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

更新

使用mvn clean install 运行测试

我正在尝试寻找相同的示例,但我没有找到任何解决方案。

【问题讨论】:

  • 所有 IT 测试都扩展 BaseITCase 吗?
  • 是的,所有测试都扩展baseit
  • Spring boot 正在尝试在端口 8080 上启动测试,即使您指定 SpringBootTest.WebEnvironment.RANDOM_PORT 也是如此。如何使用 maven 运行测试?
  • 使用 maven 全新安装命令
  • 您正在使用 maven-failsafe-plugin 并验证目标。所以要运行集成测试,你应该运行mvn clean verify

标签: spring maven spring-boot testng integration-testing


【解决方案1】:

看起来配置正确,并且服务器在端口 8008 上正常运行。我的应用程序在运行测试用例时没有选择正确的端口,导致连接被拒绝错误

由于 testng 不需要 SpringRunner,因此在 basetest 案例中进行了以下更改

 @ContextConfiguration(locations={"classpath:objectModelContext.xml"})
//@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseITCase extends com.test.common.BaseITCase {

    protected static final String baseUrl = "http://localhost:" +  port + "/test/api/";

    protected String getAdminToken(String username, String password, String deviceId) throws Exception {
    }
}

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2015-02-10
    • 2019-05-30
    • 1970-01-01
    • 2018-01-31
    • 2014-08-15
    • 1970-01-01
    • 2018-02-01
    • 2022-01-19
    相关资源
    最近更新 更多