使用jacoco进行REST调用的代码覆盖率

时间:2015-06-05 09:39:33

标签: rest client code-coverage jacoco

我使用arquillian来测试我的REST端点。这是我们正在使用的示例端点和测试用例。

REST ENDPOINT

ncat --sctp localhost 80

TEST CASE:

# Here are the packages that we need for our SCTP server                                        
import socket                                                                                   
import sctp                                                                                     
from   sctp import *                                                                            
import threading                                                                                

# Let's create a socket:                                                                        
my_tcp_socket = sctpsocket_tcp(socket.AF_INET)                                                  
my_tcp_port   = 80                                                                              

# Here are a couple of parameters for the server                                                
server_ip     = "0.0.0.0"                                                                       
backlog_conns = 3                                                                               

# Let's set up a connection:                                                                    
my_tcp_socket.events.clear()                                                                    
my_tcp_socket.bind((server_ip, my_tcp_port))                                                    
my_tcp_socket.listen(backlog_conns)                                                             

# Here's a method for handling a connection:                                                    
def handle_client(client_socket):                                                               
  client_socket.send("Howdy! What's your name?\n")                                              
  name = client_socket.recv(1024) # This might be a problem for someone with a reaaallly long name.
  name = name.strip()                                                                           

  print "His name was Robert Paulson. Er, scratch that. It was {0}.".format(name)               
  client_socket.send("Thanks for calling, {0}. Bye, now.".format(name))                         
  client_socket.close()                                                                         

# Now, let's handle an actual connection:                                                       
while True:                                                                                     
    client, addr   = my_tcp_socket.accept()                                                     
    print "Call from {0}:{1}".format(addr[0], addr[1])                                          

    client_handler = threading.Thread(target = handle_client,                                   
                                      args   = (client,))                                       
    client_handler.start() 

我将REST和NONREST元素分离为单独的shrinkwrap存档,并将其部署到服务器和测试。问题是我没有获得REST端点的jacoco覆盖。如果我取消注释testNonRESTSayHello(),它将显示该类的覆盖范围 - 仅适用于方法:nonRESTSayHello(),尽管两种方法都在执行。区别在于其余端点通过来自testcase的Invocation.Builder进行了调整。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

虽然您可以使用Jacoco进行覆盖测试,而黑盒子可以使用#。测试(见this page on how to use a java agent with tcp client/server),我想知道为什么要这样做。我的意思是,在“黑匣子”中测试休息服务'时尚只是一种测试你正在使用的其余框架和代码的方法。在这种情况下,我通常使用模拟并将其余服务作为普通方法进行测试,因为这样的测试(在我看来)更容易阅读,维护和(最后但并非最不重要)更快的运行。 编辑,添加示例:

@RunWith(Arquillian.class)
public class HelloWorldResourceTest {

  @EJB
  HelloWorldResource helloService;

  @Test
  public void testGetSayHello() {
      Response response = helloService.getSayHello();
      assertEquals(200, response.getStatus());
  }

}