单测挡板demo(jdk7)

分布式多个系统间的调用,写单测的时候需要设置挡板,调用其他系统的时候,不是真的调用,而是返回设置好的返回结果
需要的包(因为demo工程还有其他的依赖,一并拷贝了)

<dependency>
			<groupId>junitgroupId>
			<artifactId>junitartifactId>
			<version>4.10version>
			<scope>testscope>
		dependency>
		<dependency>
		    <groupId>org.powermockgroupId>
		    <artifactId>powermock-module-junit4artifactId>
		    <version>1.5.5version>
		    <scope>testscope>
		dependency>
		<dependency>
			<groupId>org.powermockgroupId>
			<artifactId>powermock-api-mockitoartifactId>
			<version>1.5.5version>
			<scope>testscope>
		dependency>
		<dependency>
		    <groupId>org.powermockgroupId>
		    <artifactId>powermock-module-junit4-rule-agentartifactId>
		    <version>1.5.5version>
		    <scope>testscope>
		dependency>
		<dependency>
		    <groupId>org.powermockgroupId>
		    <artifactId>powermock-module-javaagentartifactId>
		    <version>1.5.5version>
		    <scope>testscope>
		dependency>
		<dependency>
		    <groupId>org.springframeworkgroupId>
		    <artifactId>spring-testartifactId>
		    <version>3.2.0.RELEASEversion>
		    <scope>testscope>
		dependency>
  	<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webmvcartifactId>
			<version>3.2.0.RELEASEversion>
		dependency>

测试类1

package com.cwq.testInterface;

public class CCN {

	public static String test1(String name){
		return "test1 return " + name;
	}
	
	public static String test2(String name,String age){
		return "test1 return " + name + ", age is " + age;
	}
	
}

测试类2,用于调用测试类1

package com.cwq.testInterface;

import org.springframework.stereotype.Component;

@Component
public class CCNImple {

	public String returnString(){
		
		String t1 = CCN.test1("cwq");
		
		String t2 = CCN.test2("phq", "23");
		
		String str = "start\n"+t1 + "\n" + t2+"\nend";
		//System.out.println(str);
		
		return str;
	}
	
}

扫描组件


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
		default-autowire="byName">
	<context:component-scan base-package="com.cwq.testInterface">context:component-scan>
beans>

配置挡板,模拟设置测试类1 CCN的挡板,这样调用CCN的方法,不会执行方法体,直接返回结果(方法体中的操作都不会被执行到)

package com.cwq.testCase.utils;

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;

import com.cwq.testInterface.CCN;


public class MockUtil {

	
	
	public static void MockTest(){
		PowerMockito.mockStatic(CCN.class);
		
		Mockito.when(CCN.test1(Mockito.anyString())).thenAnswer(new Answer<String>() {

			@Override
			public String answer(InvocationOnMock invocation) throws Throwable {
				System.out.println(invocation.getArguments()[0]);
				return "I'm first";
			}
		});
		
		Mockito.when(CCN.test2(Mockito.anyString(), Mockito.anyString())).thenAnswer(new Answer<String>() {

			@Override
			public String answer(InvocationOnMock invocation) throws Throwable {
				System.out.println("test2第一个参数"+invocation.getArguments()[0]);
				System.out.println("test2第二个参数"+invocation.getArguments()[1]);
				return "I'm second";
			}
		});
	}
	
}

测试类

package com.cwq.testCase.utils;


import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.cwq.testInterface.CCN;
import com.cwq.testInterface.CCNImple;

@PrepareForTest(CCN.class)//把设置为挡板的类设置进来
@ContextConfiguration(locations={"classpath:beans.xml"})//加载xml文件
@RunWith(SpringJUnit4ClassRunner.class)//使用spring的测试类
public class TestMockito {
	
	@Autowired
	private CCNImple ccnImple;
	
	@Rule
	public PowerMockRule rule = new PowerMockRule();//虽然在该类中没有使用rule,但是必须加进来,这样才能使用Java代理生成挡板
	
	@Before
	public void init(){
		MockUtil.MockTest();//需要在执行接口之前初始化挡板
	}
	
	@Test
	public void test() {
		System.out.println(ccnImple.returnString());
	}

}

注:运行参数只要加上-noverify 即可,因为JVM在加载class的时候,会校验class,防止JVM被异常代码损坏了,下面都可以忽略了

最后需要设置运行参数,单测才能成功的运行

-XX:PermSize=200m -XX:-UseSplitVerifier -javaagent:C:/Users/xxx/.m2/repository/org/powermock/powermock-module-javaagent/1.5.5/powermock-module-javaagent-1.5.5.jar

单测挡板demo(jdk7)_第1张图片

注:如果是使用jdk8运行上面demo,则会出现错误
在这里插入图片描述
因为jdk8删除了这两个参数了,只需要将运行参数-XX:PermSize=200m -XX:-UseSplitVerifier 删除即可,但是会出现下面的错误,这是因为jdk会校验,需要加上参数-noverify 不校验
单测挡板demo(jdk7)_第2张图片

你可能感兴趣的:(java)