测试类调用实际对象,而不是模拟对象

时间:2015-02-28 06:42:06

标签: java unit-testing powermock easymock

我试图模拟外部库,但是正在使用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,这不应该发生,因为我应该嘲笑这个类。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

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