单元测试注释? [英] Unit testing annotations?

查看:171
本文介绍了单元测试注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问自己,我应该多深入(单元)测试我的课程。
例如,我有以下简单类。

I'm asking myself how deep should I go in (unit) testing my classes. As example, I have following simple class .

import javax.annotation.security.PermitAll;
import javax.ejb.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path(value = "ping")
@Singleton
@PermitAll
public class PingRestService {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String pingMethod(){
        return "pong";
    }

}

我写了以下单元测试:

import static org.junit.Assert.*;
import java.lang.reflect.Method;
import javax.annotation.security.PermitAll;
import javax.ejb.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.junit.Test;


public class PingRestServiceTest {

    PingRestService prs = new PingRestService();

    @Test
    public void testClassAnnotations(){
        assertEquals(3, prs.getClass().getAnnotations().length);

        assertTrue(prs.getClass().isAnnotationPresent(PermitAll.class));
        assertTrue(prs.getClass().isAnnotationPresent(Singleton.class));
        assertTrue(prs.getClass().isAnnotationPresent(Path.class));

        assertEquals("ping", prs.getClass().getAnnotation(Path.class).value());

    }

    @Test
    public void testPingMethodAnnotations() throws SecurityException, NoSuchMethodException{

        Method method = prs.getClass().getDeclaredMethod("pingMethod");
        assertEquals(2, method.getAnnotations().length);

        assertTrue(method.isAnnotationPresent(GET.class));
        assertTrue(method.isAnnotationPresent(Produces.class));

        assertEquals(1, method.getAnnotation(Produces.class).value().length);
        assertEquals(MediaType.TEXT_PLAIN, method.getAnnotation(Produces.class).value()[0]);
    }

    @Test
    public void testPingMethod() {
        assertEquals("pong", prs.pingMethod());
    }

}

有意义吗?
或者我应该只测试返回的字符串(pong,testPingMethod),跳过所有注释测试(testClassAnnotations,testPingMethodAnnotations)?

does it make sense? Or should I only test the returning string ("pong", testPingMethod), skipping all annotations tests (testClassAnnotations,testPingMethodAnnotations) ?

我认为有些注释是业务逻辑的一部分(例如PermitAll),因此应该进行测试。

I think some annotations are part of a business logic (e.g. PermitAll), and therefore should be tested.

推荐答案

大部分时间都会测试一下代码而不是它的实现方式。这称为黑盒测试(参见: http ://en.wikipedia.org/wiki/Black-box_testing )。
实施测试时,您应该问自己:要测试的设备的可能输入值是多少,预期结果是什么?
现在,在测试中,您使用输入值调用代码,并使用预期的结果检查结果,以确保代码按您希望的方式运行。
随着时间的推移,您可以优化代码而无需更改功能。那你就不需要改变测试了。但是你可以重新运行它以确保它仍然以相同的方式运行。即使它的实现方式不同。或者,您可能会更改实施细节,这些细节会对您测试的功能产生副作用。此外,在这种情况下,您无需更改测试,但只需重新运行即可。
在您的简单情况下,您没有输入和一个静态输出,因此您只需调用方法并检查是否返回pong。但是经过测试的真实案例很少那么简单。

Most of the time one tests the functionality of the code and not the way it is implemented. This is called Black Box Testing (see: http://en.wikipedia.org/wiki/Black-box_testing). When implementing a test you should ask yourself: "What are the possible input values of the unit to test and what are the expected results?" Now in the test you call your code with the input values and check the result with the expected one to make sure your code behaves the way you want it. Over time you might optimize the code without wanting to change the functionality. Then you should not need to change your test. But you can re-run it to make sure it still behaves the same way. Even if it is implemented differently. Or you might make change implementation details that have side effects to the functionality you tested. Also in this case you don't need to change the test but you just need to re-run it. In your simple case you have no input and one static output so you can just call the method and check if "pong" is returned. But real life cases that are tested are rarely that simple.

编辑:您可以看到 @PermitAll 的安全性配置和'@Path'配置为输入的URL路径,并在集成测试中以'Boris the Spider'和'Avi'建议的方式测试它们。但其他注释是特定于实现的。

You can see the security that @PermitAll configures and the URL path that '@Path' configures as inputs and also test them in an integration test the way 'Boris the Spider' and 'Avi' suggested. But the other annotations are implementation specific.

这篇关于单元测试注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆