title: Creating a assertClass() method in JUnit
body: <p>I'm creating a test platform for a protocol project based on Apache MINA. In MINA when you receive packets the <code>messageReceived()</code> method gets an Object. Ideally I'd like to use a JUnit method <code>assertClass()</code>, 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 <code>instanceof</code>.</p>
  
  <p>Currently I have:</p>
  
  <pre><code>public void assertClass(String msg, Class expected, Object given) {  
      if(!expected.isInstance(given)) Assert.fail(msg);  
  }
  </code></pre>
  
  <p>To call this:</p>
  
  <pre><code>assertClass("Packet type is correct", SomePacket.class, receivedPacket);
  </code></pre>
  
  <p>This works without issue, however in experimenting and playing with this my interest was peaked by the <code>instanceof</code> operator.</p>
  
  <pre><code>if (receivedPacket instanceof SomePacket) { .. }
  </code></pre>
  
  <p>How is instanceof able to use <code>SomePacket</code> to reference the object at hand? It's not an instance of an object, its not a class, what is it?! Once establishing what type <code>SomePacket</code> is at that point is it possible to extend my <code>assertClass()</code> to not have to include the <code>SomePacket.class</code> argument, instead favouring <code>SomePacket</code>?</p>