我在问自己,我应该深入(单元)测试我的类(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)?
我认为一些注释是业务逻辑的一部分(例如 PermitAll),因此应该进行测试。
最佳答案
大多数时候,人们测试的是代码的功能,而不是代码的实现方式。这称为 Black Box Testing
(参见:http://en.wikipedia.org/wiki/Black-box_testing)。
在实现测试时,您应该问自己:“要测试的单元的可能输入值是什么,预期结果是什么?”
现在在测试中,您使用输入值调用您的代码,并使用预期值检查结果,以确保您的代码按照您想要的方式运行。
随着时间的推移,您可能会优化代码而不想更改功能。那么你应该不需要改变你的测试。但是您可以重新运行它以确保它仍然以相同的方式运行。即使它的实现方式不同。或者您可能会更改对您测试的功能有副作用的实现细节。同样在这种情况下,您不需要更改测试,只需重新运行它即可。
在您的简单情况下,您没有输入和一个静态输出,因此您可以调用该方法并检查是否返回“pong”。但经过测试的真实案例很少如此简单。
编辑:您可以看到 @PermitAll
配置的安全性和“@Path”配置为输入的 URL 路径,还可以在集成测试中以“Boris the Spider”和“阿维建议道。但其他注释是特定于实现的。
关于java - 单元测试注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17261501/