我需要测试一些api。例如,我在类中有多个@Test方法来测试我的功能,在初始化之前,我先连接到服务的某些URL并使用它。
如果我的服务位于多个URL(不同的服务器环境),如何为不同的服务URL测试此功能?
流程:
当只有一位主持人时,我会这样:
public class TestAPI{
@org.junit.Before
public void init() {
service = new Service("www.test.com");
}
@org.junit.Test
public void testA(){
service.CallA();
}
@org.junit.Test
public void testB(){
service.CallB();
}
}
最佳答案
某个时候发生在我身上,他们发现了这个很棒的主意,称为参数化测试,例如:
http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/
这样,您可以同时使用不同的参数进行所有相同的测试。
关于Java单元测试用于不同的输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10028188/