TestNG + Spring集成測試

在本教程中,我們將演示如何使用TestNG測試Spring的組件。

使用的工具 :

  • TestNG 6.8.7
  • Spring 3.2.2.RELEASE
  • Maven 3
  • Eclipse IDE

1. 項目依賴

爲了演示,首先創建一個名稱爲:TestngSpringIntegration 的 Maven 項目。

要將Spring與TestNG集成,您需要spring-test.jar包依懶,添加以下內容:

創建文件: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>com.yiibai</groupId>
    <artifactId>TestngSpringIntegration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestngSpringIntegration</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.0.6.RELEASE</spring.version>
        <testng.version>6.8.7</testng.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>compile</defaultGoal>
    </build>

</project>

2. Spring組件

創建一個簡單的Spring組件,稍後將使用TestNG來測試這個組件。

創建一個Java接口文件:EmailGenerator.java ,代碼如下 -

package com.yiibai;

public interface EmailGenerator {

    public String generate();

}

創建一個Java類文件:RandomEmailGenerator.java ,代碼如下 -

package com.yiibai;

import org.springframework.stereotype.Service;

@Service
public class RandomEmailGenerator implements EmailGenerator {

    public String generate() {
        return "[email protected]";
    }

}

3. TestNG + Spring

test 文件夾中創建一個Spring配置文件,用於Spring組件掃描。

創建一個XML文件:*${project}/src/test/resources/spring-test-config.xml* ,代碼如下 -

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    ">

    <context:component-scan base-package="com.yiibai" />

</beans>

要訪問TestNG中的Spring組件,擴展AbstractTestNGSpringContextTests,請參閱以下示例:

創建一個Java文件:*${project}/src/test/java/com/yiibai/TestSpring.java* ,代碼如下 -

package com.yiibai;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.yiibai.EmailGenerator;

@Test
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
public class TestSpring extends AbstractTestNGSpringContextTests {

    @Autowired
    EmailGenerator emailGenerator;

    @Test()
    void testEmailGenerator() {

        String email = emailGenerator.generate();
        System.out.println(email);

        Assert.assertNotNull(email);
        Assert.assertEquals(email, "[email protected]");


    }

}

執行上面測試代碼,輸出以下結果 -

五月 04, 2017 00:26:07 上午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1855856090\testng-customsuite.xml

五月 04, 2017 00:26:07 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-test-config.xml]
五月 04, 2017 00:26:07 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@262b2c86: startup date [Thu May 04 00:26:07 CST 2017]; root of context hierarchy
[email protected]
PASSED: testEmailGenerator

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@49e4cb85: 12 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@5594a1b5: 6 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@5a39699c: 80 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7d417077: 34 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@7c75222b: 13 ms