Java – create the assertclass () method in JUnit
I am creating a test platform for the protocol project based on Apache Mina In Mina, when you receive a packet, the messagereceived () method gets an object Ideally, I want to use the JUnit method assertclass (), but it doesn't exist I'm trying to find the closest thing I tried to find something similar to instanceof
At present, I have:
public void assertClass(String msg,Class expected,Object given) { if(!expected.isinstance(given)) Assert.fail(msg); }
be called:
assertClass("Packet type is correct",SomePacket.class,receivedPacket);
That's no problem, but my interest was peaked by the instanceof operator when I tried and played this
if (receivedPacket instanceof SomePacket) { .. }
How does instanceof use somepacket to reference the object at hand? It is not an instance of an object, it is not a class, what is it?! Once the type of somepacket is determined, I can extend my assertclass () to not include somepacket Class parameter, but supports somepacket?
Solution
The parameter of instanceof is the class / interface name, which is directly implemented as a JVM instruction So it's very effective
http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html#instanceof
An advantage of instanceof over comparison classes is that instanceof can still work if the class has been proxied
If the object to be checked is null, it will also work