Junit java实际调用的方法返回空值

tquggr8v  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(124)

这是一个带有final和private构造函数的Utility类,该类中的所有方法都是静态的。
当调用测试方法时,它们总是返回空值。
好心帮忙。

` 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;
}}`

测试类代码

@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);
          
      }

堆栈跟踪

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);
这应该工作正常,应该返回一些真实的数据,但它不工作。请帮助我。上面的代码行给我总是空值。提前感谢。

qnzebej0

qnzebej01#

我已经得到了答案:我在做不必要的模拟,即使在真实的方法中,它实际上是在创建对象。

@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);
      
  }

相关问题