1.JUnit依赖
功能测试使用到了JUnit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
在pom.xml中添加上方的依赖
2.测试前准备
(1).定义接口JUnitTestInterface
package com.maigo.rpc.test;
import java.util.List;
public interface JUnitTestInterface
{
public String methodWithoutArg();
public String methodWithArgs(String arg1, int arg2);
public JUnitTestCustomObject methodWithCustomObject(JUnitTestCustomObject customObject);
public List<String> methodReturnList(String arg1, String arg2);
public void methodThrowException();
public void methodTimeOut();
public void methodReturnVoid();
public String methodDelayOneSecond();
public int methodForMultiThread(int threadId);
public String methodForPerformance();
}
定义了用于测试的方法
package com.maigo.rpc.test;
import java.util.Arrays;
import java.util.List;
public class JUnitTestInterfaceImpl implements JUnitTestInterface
{
public String methodWithoutArg()
{
return "this is return from methodWithoutArg()";
}
public String methodWithArgs(String arg1, int arg2)
{
return arg1 + " = " + arg2;
}
public JUnitTestCustomObject methodWithCustomObject(
JUnitTestCustomObject customObject)
{
JUnitTestCustomObject object = new JUnitTestCustomObject(customObject.getString() + " after",
customObject.getI() + 47);
return object;
}
public List<String> methodReturnList(String arg1, String arg2)
{
return Arrays.asList(arg1, arg2);
}
public void methodThrowException()
{
throw new JUnitTestCustomException();
}
public void methodTimeOut()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public void methodReturnVoid()
{
return;
}
public String methodDelayOneSecond()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return "I have sleep 1000ms already.";
}
public int methodForMultiThread(int threadId)
{
return threadId;
}
public String methodForPerformance()
{
return "Maigo";
}
}
实现了用于测试的方法
(3).定义自定义类型JUnitTestCustomObject
package com.maigo.rpc.test;
public class JUnitTestCustomObject
{
private String string;
private int i;
public JUnitTestCustomObject(String string, int i)
{
super();
this.string = string;
this.i = i;
}
public String getString()
{
return string;
}
public void setString(String string)
{
this.string = string;
}
public int getI()
{
return i;
}
public void setI(int i)
{
this.i = i;
}
@Override
public boolean equals(Object obj)
{
JUnitTestCustomObject object = null;
if(obj insta