使用spring boot和testng运行集成测试时出错 [英] error while running integration test with spring boot and testng

查看:107
本文介绍了使用spring boot和testng运行集成测试时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用testng运行集成测试,并且springboot.app运行良好,并且能够使用eclipse运行独立测试.但是当我们使用maven命令行运行测试用例时,我正在跟踪异常

We are trying to run integration test using testng and springboot.app is running fine and we are able run standalone test using eclipse.but when we run testcase using maven command line.i am getting following exception

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来运行测试用例,那么每个测试用例都将尝试加载其自己的上下文,并且测试用例将因主机未找到异常而失败.

if we try to load context.xml for running testcase then each testcase is trying to load its own context and testcase are failing with host not found exception.

基本测试用例类

//@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

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

i am trying to look for example for same but i am not finding any solution.

推荐答案

看起来配置正确,并且服务器在端口8008上正确运行.我的应用在运行测试用例时给出了拒绝连接错误,未选择正确的端口

looks like configuration is correct and server is runnig proplerly on port 8008.my app was not picking correct port while running test cases giving connection refused error

在基本测试的情况下进行了以下更改,因为testng不需要SpringRunner

following changes were done in basetest case since SpringRunner is not required with testng

 @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 {
    }
}

这篇关于使用spring boot和testng运行集成测试时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆