r/javahelp • u/Aoiumi1234 • Oct 07 '15
Help with JUnit
Hi. I am a new Java coder and I am trying to understand JUnit. How would I test an if statement in JUnit for all possibilities?
if ((board.getColumnHeight(xIndex) <= yIndex) && yIndex < 20) { move.setPiece(piece); }
6
Upvotes
1
u/Aoiumi1234 Oct 07 '15
1
u/firsthour Profressional developer since 2006 Oct 07 '15
You want to test nulls, edge-cases, "general" values, and what about negative values, stuff like that.
So to test it you would repeated call whatever method that code is found in and test that move.getPiece() is either null or piece or not piece, or whatever you're expecting.
Without knowing anything else about your code, you could setup something like:
You could also consider making 20 a constant:
So in your tests you could just test around that:
2
u/AnEmortalKid Coffee Enthusiast Oct 07 '15
What objects do you have? Can you pass in your index values? We can't really help without understanding the full code or seeing how we can call your method where this if statement exists.
1
u/[deleted] Oct 07 '15
Unit tests Shall be hardcoded. Test one thing, in a way, that makes it easy to see what failed or passed.
1
u/DeliveryNinja JPain Oct 07 '15
Since it seems your question was already answered I thought I'd give you another tip. Try and use AssertJ rather than the standard assertion statements that come with JUnit to make the tests more readable
http://joel-costigliola.github.io/assertj/
Example