Answer the question
In order to leave comments, you need to log in
How to test throwing an exception by a method that checks an internal property of an object, if it is also checked in the constructor?
Good afternoon!
I have a class whose constructor looks like this:
public ImageCutter(int width, int height, int sizing)
{
if (sizing != SIZE_CUT && sizing != SIZE_COVER && sizing != SIZE_CONTAIN)
throw new IllegalArgumentException(String.format("Image sizing method = %d not implemented.", sizing));
...
}
public ImageCopyRegion getCopyRegion(int imageWidth, int imageHeight)
{
switch(sizing)
{
case SIZE_CUT:
return getCuttedImage(imageWidth, imageHeight);
case SIZE_COVER:
return getCoveredImage(imageWidth, imageHeight);
case SIZE_CONTAIN:
return getContainedImage(imageWidth, imageHeight);
}
//TODO: как это протестировать, при условии того, что конструктор обрубил это?
throw new IllegalStateException(String.format("Image sizing method = %d not implemented", sizing));
}
@Test(expected = IllegalStateException.class)
public void testGetCopyRegion_ForInvalidSizing_MustExcept()
{
ImageCutter cutter = new ImageCutter(0, 0, 100); // будет исключение IllegalArgumentException здесь, а не ниже
cutter.getCopyRegion(-1, 0); // ожидается исключение здесь
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question