• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to test some methods on a String in Java  RSS feed

 
Garrett McClure
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a project that, in total, should have 22 tests (per my professor) and tests a class that has methods in inserting and deleting on a string (in my case, StringBuildeR), and also undoing a previous choice. All of that is good -- the code for the class works; but I am having trouble coming up with the other tests needed. I currently have 14:



Also, when I asked my professor for a bit of help, she said to "think about inserting on an empty String, a String in the middle, and a String on the end (technically, appending). Also, think about errors. Do you catch if you try to insert on a negative position or a position that doesn't exist (like 50, assuming your smart string is only 20 characters, or so). Then do the same for delete."

But I've done this, and I think the 14 tests that I have basically cover what she says. But what about the other 8? What else am I missing?
 
Garrett McClure
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just FYI: the insert tests are completed. I'm only missing some tests for delete, undo, and insert/undo.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's strange to me that your professor would have such a specific requirement for number of tests. For me it's not about quantity as much as it is about quality of the tests.

That said, the quality of your tests are quite poor. For starters, the names you give to your test methods are not helpful. Names should communicate the purpose/intent of the things they are assigned to.  What does the name testinsert1 communicate? Nothing useful really. Also, you actually have two different tests in that one method. If I were to write that test, I would have something like this instead:


If you break up some of your methods that are testing more than one thing, you might get up to that "magic" 22 tests. Again, I really don't understand why it has to be 22 but then again, it might be that if you covered the points in the guidelines/tip that she gave you that's exactly the minimum number of tests you'd end up with.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your calls to fail() in the methods where you're testing for exceptions are not that helpful either. Be more specific about what is expected, like "Expected exception when index out of bounds" or "Expected exception when null".  The message you have right now, "Invalid Smart String action not detected" does not really help explain the condition that was expected and is redundant with the @Test(expected=InvalidSmartStringException.class) annotation. Give more detail with the fail() message.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't fix those fail calls, you should remove them. They will never be reached. The @Test(expected = ...) will already cause a test failure if the exception is not thrown.

Check out the "ExpectedException Rule" section of https://github.com/junit-team/junit4/wiki/Exception-testing for some more advanced exception testing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!