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 [email protected]2b2c86: 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 [email protected]: 12 ms
[TestNG] Time taken by [email protected]: 6 ms
[TestNG] Time taken by [email protected]: 80 ms
[TestNG] Time taken by org[email protected]: 34 ms
[TestNG] Time taken by [email protected]: 13 ms