JUnit test



  • Some onshore'd offshore guy wrote a class and a JUnit test for it:

    public class TheClass<T> {
    private T data;
    public void setData(T value) {
    data = value;
    }
    public T getData() {
    return data;
    }
    }

    // JUnit
    public void testTheClass() {
    TheClass<Integer> tci = new TheClass<Integer>();
    tci.setData(1);
    Object goti = tci.getData();
    assertEquals(Integer.class, goti.getClass());

    TheClass<String> tcs = new TheClass<String>();
    tcs.setData("1");
    Object gots = tcs.getData();
    assertEquals(String.class, gots.getClass());
    }

    Forgive me, but I think that if you're going to do something, you should do it right. What about all the other possible types that you could use?

    Rookie!



  • You could probably use reflection in the testing phase to generate all known types. But then you might need JUnit tests for your JUnit tests too...



  • Looks like he doesn't really trust his templates huh.

    Oh I see the WTF, it's obviously missing the assertion below

    TheClass<Boolean> tcb = new TheClass<Boolean>();
    tcb.setData(FILE_NOT_FOUND);
    Object gotb = tcb.getData();
    assertEquals(Boolean.class, gotb.getClass());


  • No the real WTF is he was expecting this code to actually have some class just by telling it to get class.



  •  You can never test thoroughly enough!



  • I hope he also tested the actual return result. That would be something like

     

    public void testAllInt() {

      TheClass<Integer> tci = new TheClass<Integer>();
     
      for (int i = -20000000; i < 200000000; i++) {
          tci.setData(i);
          assertEquals(i, tci.getData());
      } 

    }



  • It looks like maybe he didn't understand type erasure.



  • @snoofle said:

    Forgive me, but I think that if you're going to do something, you should do it right. What about all the other possible types that you could use?

    Well, if something isn't needed, better do it damn right.. 


Log in to reply