【问题标题】:Tested class is calling actual object, instead of mocked object测试类正在调用实际对象,而不是模拟对象
【发布时间】:2015-02-28 15:53:06
【问题描述】:

我正在尝试模拟一个外部库,但是使用的是在 APKDecompiler 中创建的实际对象,而不是模拟对象。

测试代码

import com.googlecode.dex2jar.v3.Dex2jar;
import jd.core.Decompiler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import APKDecompiler;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;
import static org.powermock.api.easymock.PowerMock.*;

import java.io.File;
import java.io.IOException;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Dex2jar.class})
public class TestAPKDecompiler {
   //As this only uses external libraries, I will only test that they are called correctly by mocking them.
    @Test
    public void testAPKDecompiler() {
        try {
            File testFile = new File("ApkExtractor/src/test/resources/testApp.jar");
            String expectedDirectory = testFile.getAbsolutePath().substring(0, testFile.getAbsolutePath().length() - 4);
            mockStatic(Dex2jar.class);
            Dex2jar mockApkToProcess = createMock(Dex2jar.class);
            Decompiler mockDecompiler = createNiceMockAndExpectNew(Decompiler.class);


            expect(Dex2jar.from(testFile)).andStubReturn(mockApkToProcess);

            mockApkToProcess.to(new File(expectedDirectory + ".jar"));
            expectLastCall();

            PowerMock.expectNew(Decompiler.class).andReturn(mockDecompiler).anyTimes();

            expect(mockDecompiler.decompileToDir(expectedDirectory + ".jar", expectedDirectory)).andReturn(0);


            replay(mockApkToProcess);
            PowerMock.replay(mockDecompiler);
            replayAll();
            String actualDirectory = APKDecompiler.decompileAPKToDirectory(testFile);

            verify(mockApkToProcess);
            verify(mockDecompiler);
            verifyAll();

            assertEquals(expectedDirectory, actualDirectory);
            testFile.delete();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

类代码

import com.googlecode.dex2jar.v3.Dex2jar;
import jd.core.Decompiler;
import jd.core.DecompilerException;

import java.io.File;
import java.io.IOException;

public class APKDecompiler {
    public static String decompileAPKToDirectory(File filename) throws IOException, DecompilerException {
        String filenameWithoutFileExtension = filename.getAbsolutePath().substring(0, filename.getAbsolutePath().length() - 4);
        Dex2jar apkToProcess = Dex2jar.from(filename);
        File jar = new File(filenameWithoutFileExtension + ".jar");
        apkToProcess.to(jar);
        Decompiler decompiler = new Decompiler();

        decompiler.decompileToDir(filenameWithoutFileExtension + ".jar", filenameWithoutFileExtension);

        return filenameWithoutFileExtension;
    }

我已经尝试过了,但我没有任何运气。 EasyMock: Mocked object is calling actual method

我在调用 decompiler.decompileToDir 时收到 FileNotFoundException,这不应该发生,因为我应该模拟类。

任何帮助将不胜感激。

【问题讨论】:

    标签: java unit-testing powermock easymock


    【解决方案1】:

    答案是我没有在 @PrepareForTest 注释中包含我正在测试的类。

    @PrepareForTest({APKDecompiler.class, Dex2jar.class, Decompiler.class})
    

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      相关资源
      最近更新 更多