鍍金池/ 教程/ Java/ TestNG + Spring集成測(cè)試
TestNG參數(shù)測(cè)試(DataProvider)
TestNG配置注解實(shí)例
TestNG預(yù)期異常測(cè)試
TestNG + Selenium負(fù)載測(cè)試
TestNG參數(shù)化測(cè)試
TestNG參數(shù)測(cè)試(XML和DataProvider)
TestNG配置注釋
TestNG套件測(cè)試
TestNG依賴測(cè)試
TestNG忽略測(cè)試
TestNG忽略測(cè)試
TestNG分組測(cè)試
TestNG預(yù)期異常測(cè)試
TestNG + Selenium
TestNG參數(shù)測(cè)試實(shí)例
TestNG超時(shí)測(cè)試
TestNG Hello World入門示例
JUnit 4 Vs TestNG比較
TestNG + Spring集成示例
TestNG分組測(cè)試
TestNG教程
TestNG超時(shí)測(cè)試
JUnit 4 Vs TestNG比較
TestNG基本注解
TestNG依賴性測(cè)試
TestNG套件測(cè)試
TestNG + Spring集成測(cè)試

TestNG + Spring集成測(cè)試

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

使用的工具 :

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

1. 項(xiàng)目依賴

為了演示,首先創(chuàng)建一個(gè)名稱為:TestngSpringIntegration 的 Maven 項(xiàng)目。

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

創(chuàng)建文件: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組件

創(chuàng)建一個(gè)簡(jiǎn)單的Spring組件,稍后將使用TestNG來(lái)測(cè)試這個(gè)組件。

創(chuàng)建一個(gè)Java接口文件:EmailGenerator.java ,代碼如下 -

package com.yiibai;

public interface EmailGenerator {

    public String generate();

}

創(chuàng)建一個(gè)Java類文件:RandomEmailGenerator.java ,代碼如下 -

package com.yiibai;

import org.springframework.stereotype.Service;

@Service
public class RandomEmailGenerator implements EmailGenerator {

    public String generate() {
        return "feedback@yiibai.com";
    }

}

3. TestNG + Spring

test 文件夾中創(chuàng)建一個(gè)Spring配置文件,用于Spring組件掃描。

創(chuàng)建一個(gè)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>

要訪問(wèn)TestNG中的Spring組件,擴(kuò)展AbstractTestNGSpringContextTests,請(qǐng)參閱以下示例:

創(chuàng)建一個(gè)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, "feedback@yiibai.com");


    }

}

執(zhí)行上面測(cè)試代碼,輸出以下結(jié)果 -

五月 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
feedback@yiibai.com
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