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

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

问题描述

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

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";
    }

}

我编写了以下单元测试:

I wrote following unit test:

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"和Boris the Spider"的方式在集成测试中测试它们阿维建议道.但其他注释是特定于实现的.

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天全站免登陆