java 在 JUnit 中创建 assertClass() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/958460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:33:51  来源:igfitidea点击:

Creating a assertClass() method in JUnit

javajunit

提问by Mike

I'm creating a test platform for a protocol project based on Apache MINA. In MINA when you receive packets the messageReceived()method gets an Object. Ideally I'd like to use a JUnit method assertClass(), however it doesn't exist. I'm playing around trying to work out what is the closest I can get. I'm trying to find something similar to instanceof.

我正在为基于 Apache MINA 的协议项目创建一个测试平台。在 MINA 中,当您收到数据包时,该messageReceived()方法会获取一个对象。理想情况下,我想使用 JUnit 方法assertClass(),但它不存在。我正在四处寻找我能得到的最接近的东西。我试图找到类似的东西instanceof

Currently I have:

目前我有:

public void assertClass(String msg, Class expected, Object given) {  
    if(!expected.isInstance(given)) Assert.fail(msg);  
}

To call this:

调用这个:

assertClass("Packet type is correct", SomePacket.class, receivedPacket);

This works without issue, however in experimenting and playing with this my interest was peaked by the instanceofoperator.

这没有问题,但是在试验和玩这个时,我的兴趣被instanceof操作员达到了顶峰。

if (receivedPacket instanceof SomePacket) { .. }

How is instanceof able to use SomePacketto reference the object at hand? It's not an instance of an object, its not a class, what is it?! Once establishing what type SomePacketis at that point is it possible to extend my assertClass()to not have to include the SomePacket.classargument, instead favouring SomePacket?

instanceof 如何能够用来SomePacket引用手头的对象?它不是对象的实例,也不是类,它是什么?!一旦确定SomePacket此时是什么类型,是否可以将 my 扩展assertClass()为不必包含SomePacket.class参数,而是偏向于SomePacket

采纳答案by objects

The argument to instanceof is a class/interface name, and it is implemented directly as a JVM instruction. As such it is pretty efficient.

instanceof 的参数是一个类/接口名称,它直接作为 JVM 指令实现。因此,它非常有效。

http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html#instanceof

http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html#instanceof

One advantage of instanceof instead of comparing the classes is that instanceof will still work if the class has been proxied.

使用 instanceof 而不是比较类的一个优点是,如果类已被代理,instanceof 仍然可以工作。

It will also work if the object being checked is null.

如果被检查的对象为空,它也将起作用。

回答by Steve Freeman

Take a look at the Hamcrest matchers, now included in JUnit. What you want is something like:

看看现在包含在 JUnit 中的 Hamcrest 匹配器。你想要的是这样的:

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.instanceOf;

assertThat(receivedPacket, instanceOf(SomePacket.class));

That does everything you need, including a decent error message upon assertion failure.

这可以完成您需要的一切,包括断言失败时的体面错误消息。

回答by Cem Catikkas

Why don't you just use assertEquals?

你为什么不直接使用assertEquals

assertEquals(SomePacket.class, given.getClass());

回答by Yishai

SomePacket is a type, like this:

SomePacket 是一种类型,如下所示:

 SomePacket something = null;

You cannot reference it directly at runtime in the way that you want. The class object is the only way to go that I know of.

您不能在运行时以您想要的方式直接引用它。类对象是我所知道的唯一途径。

回答by Huxi

FYI, you could check

仅供参考,你可以检查

if(given == null || given.getClass() != expected)

to check if the class of givenis exactlythe class expected, not just assignment-compatible.
I'd call this one assertClass and your original suggestion assertInstance or assertInstanceOf.

检查 classgiven是否正是class expected,而不仅仅是赋值兼容。
我将这称为 assertClass 和您最初的建议 assertInstance 或 assertInstanceOf。

In response to the comment:
My answer was meant as a general tip, not as an answer to your original question. I added an "FYI" to make that clearer ;)

回应评论:
我的回答是一般提示,而不是对您原始问题的回答。我添加了一个“仅供参考”以使其更清楚;)

Depending on what exactly you want, both ways may be reasonable.

根据您的确切需求,两种方式都可能是合理的。

In the following example

在下面的例子中

public class Base {}
public interface Ifc {}

public class A{} extends Base implements Ifc
public class B{} extends Base

the behavior of the methods above would be like this

上述方法的行为将是这样的

A a=new A();
B b=new B();

assertClass(A.class, a);
assertClass(Base.class, a); // fails, a.getClass() != Base.class
assertClass(Ifc.class, a); // fails, a.getClass() != Ifc.class
assertClass(B.class, b);
assertClass(Base.class, b); // fails, b.getClass() != Base.class
assertClass(Ifc.class, b); // fails, b.getClass() != Ifc.class

assertInstance(A.class, a);
assertInstance(Base.class, a); // works
assertInstance(Ifc.class, a); // works
assertInstance(B.class, b);
assertInstance(Base.class, b); // works
assertInstance(Ifc.class, b); // fails, B does not implement Ifc

Most likely you'll want to check with your method.
But if you want to validate the exactclass, e.g. to make sure that it's notproxied or that it's notan extending class, my suggestion would be the way to do that.

您很可能想检查您的方法。
但是如果你想验证确切的类,例如确保它没有被代理或者它不是一个扩展类,我的建议就是这样做。

回答by Peter Isberg

assertThat(expected).isInstanceOf(given.class);

Using FEST Assertions 2.0 is a Java library Fixtures for Easy Software Testing found here: https://github.com/alexruiz/fest-assert-2.x/wiki

使用 FEST Assertions 2.0 是一个用于轻松软件测试的 Java 库夹具,可在此处找到:https: //github.com/alexruiz/fest-assert-2.x/wiki