Java 如何在测试中调用setup方法来停止重新编写相同的代码

Java 如何在测试中调用setup方法来停止重新编写相同的代码,java,Java,我正在使用java,我试图在测试中调用一个方法来清理代码。最好的方法是什么。是调用该方法还是在我创建的安装方法上使用@Before。正如您将从下面的代码中看到的,有几个重复。前进的最佳途径是什么 import com.pubnub.api.PubnubException; import org.junit.Before; import org.junit.Test; import service.PubnubService; /** * Created by peterki on 07/09

我正在使用java,我试图在测试中调用一个方法来清理代码。最好的方法是什么。是调用该方法还是在我创建的安装方法上使用@Before。正如您将从下面的代码中看到的,有几个重复。前进的最佳途径是什么

import com.pubnub.api.PubnubException;
import org.junit.Before;
import org.junit.Test;
import service.PubnubService;

/**
 * Created by peterki on 07/09/2016.
 */
public class PublisherTest {

    private PubnubService service = new PubnubService();

    @Before
    public void setupConnection() throws PubnubException {

        // Setup Subscriber
        service.subscribe("my_channel");

        // Do nothing until the subscriber has connected.
        do{} while (!service.isConnected());

    }

    @Test
    public void testPublisher() throws PubnubException {

        // Setup Subscriber
        service.subscribe("my_channel");

        // Do nothing until the subscriber has connected.
        do{} while (!service.isConnected());

        // Send 10 messages
        for(int i = 0; i <= 10; i++){
            service.publish("my_channel", "Message: " + i);
        }

        // Wait until we have recieved the 10 messages
        do{}while(service.count() <= 10);

        // For each message print out the details
        service.getMessages().forEach(System.out::println);
    }

    @Test
    public void testSportMessageType() throws PubnubException {
        // Setup Subscriber
        service.subscribe("my_channel");

        // Wait for Connection
        do{} while (!service.isConnected());

        // Publish to External Service

        //Wait till we receive the message

        // Assert the message is what we want
    }
}
import com.pubnub.api.pubNubeException;
导入org.junit.Before;
导入org.junit.Test;
导入服务.PubnubService;
/**
*由peterki于2016年9月7日创建。
*/
公共类PublisherTest{
private PubnubService service=new PubnubService();
@以前
public void setupConnection()引发PubNubeException{
//安装用户
订阅服务(“我的频道”);
//在订阅服务器连接之前,不要执行任何操作。
do{}while(!service.isConnected());
}
@试验
public void testPublisher()抛出pubNubeException{
//安装用户
订阅服务(“我的频道”);
//在订阅服务器连接之前,不要执行任何操作。
do{}while(!service.isConnected());
//发送10条信息

for(inti=0;i
在每个
@Test
之前被调用。换句话说,它是一个完美的地方,可以在测试运行之前进行准备


我认为
服务.subscribe(“my_channel”)
是在每次
@Test
之前调用
之前的
中的完美选择。换句话说,它是一个完美的地方,可以在测试运行之前做一些必须准备的事情


我想说
service.subscribe(“my_channel”)
非常适合在
之前的
中使用,测试用例中的公共代码可以很容易地提取到从每个单独测试调用的方法中(或者-如果所有测试中都存在相同的公共代码-只需将它们添加到
之前的
>)

但是您的测试将以当前编写的方式变得脆弱。如果服务从未连接(可能是因为服务代码中引入了新的bug),会发生什么情况?您的测试将无限期挂起,而不是失败


我会推荐这种东西。

测试用例中的公共代码可以很容易地提取到从每个单独的测试中调用的方法中(或者-如果所有测试中都存在相同的公共代码-只需将它们添加到
@之前的
>)

但是您的测试将以当前编写的方式变得脆弱。如果服务从未连接(可能是因为服务代码中引入了新的bug),会发生什么情况?您的测试将无限期挂起,而不是失败


我建议您使用这类服务。

如果您希望只建立一次服务(如DB连接),可以使用
@BeforeClass
而不是
@BeforeClass
否则
@Before
是完美的。

如果您希望您的服务只建立一次(如DB连接),则可以使用
@BeforeClass
而不是
@Before
,否则
@Before
是完美的