Dubbo接口测试
- pom文件中引入dubbo依赖和服务jar包依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.ceba</groupId>
<artifactId>dubbotestdemo</artifactId>
<scope>system</scope>
<systemPath>${basedir}/lib/dubbotestdemo.jar</systemPath>
</dependency>
- 配置consumer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
\thttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="3600000" />
<dubbo:reference id="dubboTest" interface="com.provider.service.DubboDemo"/>
</beans>
- 编写用例(引入服务jar包的方式)
import com.provider.service.DubboDemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
* @author by LiHe on 2018/8/26.10:37
*/
public class DubboTestDemo1 {
ApplicationContext context;
@BeforeMethod
public void setUp() {
context = new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-consumer.xml"});
}
@Test
public void testDubblo() {
DubboDemo dubboDemo = (DubboDemo) context.getBean("dubboTest");
String res = dubboDemo.sayHello("lihe");
assertThat(res,containsString("lihe"));
System.out.println(res);
}
}
- 编写用例(spring-test方式)
4.1 引入Spring-test jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
4.2 进一步优化测试代码
import com.provider.service.DubboDemo;
import com.sun.tools.javac.file.Locations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
/**
* @author by LiHe on 2018/8/26.12:12
*/
@ContextConfiguration(locations={"classpath:dubbo-consumer.xml"})
public class DubboSpringTest extends AbstractTestNGSpringContextTests {
@Autowired
DubboDemo dubboDemo;
@Test
public void testDubbo() {
String res = dubboDemo.sayHello("lihe");
System.out.println(res);
}
}
- 编写用例(反射方式)
引入zkclient.jar
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
import com.alibaba.dubbo.rpc.service.GenericService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author by LiHe on 2018/8/26.14:24
*/
public class DubboInvokeTest {
private ApplicationContext context;
@BeforeMethod
public void setUp() {
context=new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-consumer.xml"});
}
@Test
public void testDubboInvoke() {
GenericService genericService=(GenericService) context.getBean("dubboTest");
Object res =genericService.$invoke("sayHello",new String[]{"java.lang.String"},new Object[]{"lihe"});
System.out.println(res.toString());
}
}
jmeter javasampleclient脚本编写
import io.restassured.mapper.factory.JAXBObjectMapperFactory;
import io.restassured.response.Response;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import static io.restassured.RestAssured.given;
/**
* @author by LiHe on 2018/8/26.16:14
*/
public class TestJmeterDemo extends AbstractJavaSamplerClient {
String corpId;
String corpSecret;
@Override
public void setupTest(JavaSamplerContext context) {
corpId= context.getParameter("corpid");
corpSecret= context.getParameter("corpSecret");
System.out.println("测试开始");
}
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult result = new SampleResult();
result.sampleStart();
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
Response res = given().param("corpid", corpId).param("corpSecret", corpSecret).get(url).prettyPeek();
String accessToken = res.getBody().jsonPath().getString("access_token");
System.out.println("accessToken = " + accessToken);
if (res.getStatusCode() == 200) {
result.setSuccessful(true);
result.setResponseData(res.getBody().prettyPrint(), "UTF-8");
} else {
result.setSuccessful(false);
}
result.sampleEnd();
return result;
}
@Override
public Arguments getDefaultParameters() {
Arguments arguments = new Arguments();
arguments.addArgument("corpid", "");
arguments.addArgument("corpSecret", "");
return arguments;
}
@Override
public void teardownTest(JavaSamplerContext context) {
System.out.println("测试结束");
}
}
依赖jar包
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_java -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>4.0</version>
</dependency>
打包配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>io.rest-assured</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>