Reputation: 51
This is an Utility class with final and private constructor all methods in this class are static.
When called the methods for testing they returned null value always.
Kindly Help.
` public final class ImageUtil {
final static Logger logger = LoggerFactory.getLogger(ImageUtil.class);
private ImageUtil() {}
private static final int MAX_MASTER_WIDTH = 2000;
private static final int MIN_MASTER_WIDTH = 500;
private static final int MAX_SAMPLE_WIDTH = 400;
public static BufferedImage scaleImage(BufferedImage image, int maxWidth, int type){
int oldWidth = image.getWidth();
int oldHeight = image.getHeight();
int newHeight = (int) Math.round((double) maxWidth / (double) oldWidth * (double) oldHeight);
BufferedImage resized = new BufferedImage(maxWidth, newHeight, type);
Graphics2D g = resized.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, maxWidth, newHeight, 0, 0, image.getWidth(), image.getHeight(), null);
g.dispose();
return resized;
}}`
TestClass Code
@PrepareForTest(value = {FileUtil.class,JpegReader.class,ImageUtil.class,System.class,FileUtils.class,JpegReader.class,ImageIO.class})
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class ImageUtilTest {
MockedStatic<ImageUtil> imageUtil= null ;
File tempFile = null;
@ClassRule
TemporaryFolder temporaryFolder ;
@BeforeEach
public void init() throws IOException {
imageUtil= mockStatic(ImageUtil.class);
tempFile = mock(File.class);
temporaryFolder = new TemporaryFolder();
temporaryFolder.create();
}
@AfterEach
public void afterEach() {
imageUtil.close();
tempFile.delete();
temporaryFolder.delete();
}
@Test
@DisplayName("scaleImageTest")
public void scaleImageTest() throws Exception{
int oldWidth = 200;
int oldHeight = 150;
int newHeight = (int) Math.round((double) 200 / (double) oldWidth * (double) oldHeight);
BufferedImage mockedImage = mock(BufferedImage.class) ;
BufferedImage mockedNewImage = mock(BufferedImage.class) ;
Graphics2D mockedGraphics = mock(Graphics2D.class);
when(mockedImage.getWidth()).thenReturn(oldWidth);
when(mockedImage.getHeight()).thenReturn(oldHeight);
when(mockedImage.getWidth()).thenReturn(200);
Mockito.doNothing().when(mockedGraphics).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
when(mockedGraphics.drawImage(mockedImage, 0, 0, 200, newHeight, 0, 0, oldHeight,oldWidth, null)).thenReturn(true);
Mockito.doNothing().when(mockedGraphics).dispose();
PowerMockito.whenNew(BufferedImage.class).withArguments(oldWidth, oldHeight,newHeight).thenReturn(mockedNewImage);
when(mockedNewImage.createGraphics()).thenReturn(mockedGraphics);
BufferedImage actualImage = ImageUtil.scaleImage(mockedImage, oldWidth, newHeight);
assertEquals(mockedNewImage, actualImage);
}
Stacktrace
org.opentest4j.AssertionFailedError: expected: <Mock for BufferedImage, hashCode: 307036850> but was: <null>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1141)
at test.com.renoworks.framework.util.ImageUtilTest.scaleImageTest(ImageUtilTest.java:234)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
BufferedImage actualImage = ImageUtil.scaleImage(mockedImage, oldWidth, newHeight);
This should work properly and should return some real data, but its not working . Please help me . Above code of line giving me always null value. Thanks in advance.
Upvotes: 1
Views: 461
Reputation: 51
I have got the answer: I am doing unnecessary mocking even in real method its actually creating objects. This is the working code:
@Test
public void scaleImageTest() throws Exception{
// Setup
int oldWidth = 200;
int oldHeight = 150;
BufferedImage buffImage = new BufferedImage(oldWidth, oldHeight, 1);
// imageUtil this variable is mockedStatic
imageUtil.when(()->ImageUtil.scaleImage(buffImage, oldWidth, 1)).thenCallRealMethod();
// Run the test
BufferedImage actualImage = ImageUtil.scaleImage(buffImage, oldWidth, 1);
// Verify the results
assertNotNull( actualImage);
}
Upvotes: 1