Say you have a public method like this
public Sprite spriteAt(int x, int y)
{
assert withinBorders(x, y) : "PRE: x and y should be inside the borders";
return tileAt(x, y).topSprite();
}
Against
public Sprite spriteAt(int x, int y)
{
if (!withinBorders(x, y)) throw new InvalidArgumentException();
return tileAt(x, y).topSprite();
}
In the bottom case, I usually had a unit test case that checks if an exception is thrown when an invalid value for x and / or y is given as:
@Test(expected = InvalidArgumentException.class)
public void SpriteAt_InvalidX_ThrowsInvalidArgumentException()
{
sut.spriteAt(-100, 0);
}
This test case should ensure that the argument validation logic is implemented in the method.
However, for the approval method above, I'm not sure what I should do. Claims are not production code, and I think that means that I do not need to check the statements.
, , , . , , ( ), , , .
, , , junit , . .
@Test
public void SpriteAt_InvalidX_AssertionThrowsException()
{
if (assertion is enabled)
{
try
{
sut.spriteAt(-100, 0);
fail();
}
catch (AssertionFailureException e)
{
}
}
}
, . , . , ? , ?