TestNG+Spring集成
Posted 测试工程师成长之道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TestNG+Spring集成相关的知识,希望对你有一定的参考价值。
点击箭头处“蓝色字”,关注我哦!!
Testng+spring
首先来看下创建项目依赖包,我们仍旧使用Maven来管理,其中最重要的是
spring-test.jar
我们来看下pom.xml
<groupId>TestngSpring</groupId>
<artifactId>TestngSpring</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
创建一个Spring java接口
public interface EmailGenerator {
public String generate();
}
在创建一个java类
import org.springframework.stereotype.Service;
public class RandomEmailGenerator implements EmailGenerator {
public String generate() {
return "zhou@126.com";
}
}
创建一个测试类
"classpath*:/spring-test-config.xml"}) (locations = {
public class TestSpring extends AbstractTestNGSpringContextTests{
EmailGenerator emailGenerator;
()
void testEmailGeneration(){
String email=emailGenerator.generate();
System.out.println(email);
Assert.assertNotNull(email);
Assert.assertEquals(email,"zhou@126.com");
}
}
在类路径下创建spring-test 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="TestngSpring"/>
</beans>
执行结果:
[ ] ------------------------------------------------------------------------
[ ]
[2.6:resources (default-resources) @ TestngSpring --- ] --- maven-resources-plugin:
['UTF-8' encoding to copy filtered resources. ] Using
[0 resource ] Copying
[ ]
[3.1:compile (default-compile) @ TestngSpring --- ] --- maven-compiler-plugin:
[ ] Changes detected - recompiling the module!
[2 source files to /Users/louiezhou/home/intellijS2/TestngSpring/target/classes ] Compiling
[ ] ------------------------------------------------------------------------
[ ] BUILD SUCCESS
[ ] ------------------------------------------------------------------------
[1.925 s ] Total time:
[2019-11-20T13:26:39+08:00 ] Finished at:
[16M/304M ] Final Memory:
[ ] ------------------------------------------------------------------------
Process finished with exit code 0
伟大教育家孔子说过,"知行合一,学以致用", 才是做好学问的终极法宝.
方便拉你进入
扫码关注
以上是关于TestNG+Spring集成的主要内容,如果未能解决你的问题,请参考以下文章