使用gradle运行时,Stubbing无法正常工作

时间:2018-06-10 12:42:50

标签: java mockito build.gradle powermockito

我为我班上写了一个考试。当我使用intelliJ运行测试方法时测试工作文件(右键单击方法并使用运行测试)。但是当我使用gradle

运行时,它不起作用

以下是我要测试的课程

public class FeedOperations {
    private static final FeedOperations INSTANCE = new FeedOperations();
    private Request request;
    private Configurations configurations;
    private FeedOperations(){
        request = Request.getInstance();
        configurations = Configurations.getInstance();
    } 

    public List<Feed> getFeeds(int pageNumber) throws Exception {
        if(pageNumber < 1) {
            throw new IllegalArgumentException("Page number can't be less than 1");
        }
        List<Feed> feeds = null;
        String url = configurations.getGetFeedsUrl();
        url = String.format(url, pageNumber+"");
        System.out.println("calling do get");
        System.out.println("url:"+ url);
        String response = request.doGet(url, null);
        System.out.println("Response:"+response);
        Gson gson = GsonUtil.getGson();
        FeedListWrapper wrapper = gson.fromJson(response, FeedListWrapper.class);
        if(wrapper != null)
            feeds = wrapper.getFeeds();
        return feeds;
    }
}

请求类包含用于发出http请求的实用程序方法,我在我的测试类中进行模拟。 以下是我的测试类

@RunWith(PowerMockRunner.class)
@PrepareForTest(Request.class)
public class FeedOperationsTest {


    @Mock
    private Request request;

    private Configurations configurations;

    private FeedOperations feedOperations;


    @Before
    public void setup() {
        Whitebox.setInternalState(Request.class, "instance", this.request);

        Configurations.getInstance();
        UserTokens tokens = new UserTokens();
        tokens.setAuthKey("test api");
        PixyfiSDK.init("localhost", tokens, "TEST");
        configurations = Configurations.getInstance();
        feedOperations = FeedOperations.getInstance();
        try {
            when(request.doPost(anyString(), ArgumentMatchers.<String, String>anyMap(),
                    ArgumentMatchers.<String, String>anyMap())).thenCallRealMethod();
            when(request.doGet(anyString(), ArgumentMatchers.<String, String>anyMap()))
                    .thenCallRealMethod();
            when(request.doGet(anyString(), ArgumentMatchers.<String, String>anyMap(),
                    anyBoolean())).thenCallRealMethod();
            System.out.println("call real method configured");
        } catch (PixyfiException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getFeeds() throws Exception {
        String url = String.format(configurations.getGetFeedsUrl(), 1);        
        System.out.println("url test:"+url);
        when(request.doGet(url, null)).thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                System.out.println("Called from answer");
                return ApiResponses.GET_FEEDS_RESPONSE;
            }
        });

        List<Feed> feeds = feedOperations.getFeeds(1);
        Assert.assertNotNull("Feed list is null", feeds);
        Assert.assertTrue("feed list is empty", feeds.size() > 0);
    }
}

答案永远不会被称为Request class的真正方法

下面是我的gradle.build

apply plugin: 'java-library'

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.mashape.unirest:unirest-java:1.4.9'
    implementation 'log4j:log4j:1.2.17'
    implementation project(':pixyfi-data')
    implementation project(':utility')
    compile 'com.google.code.gson:gson:2.6.2'
    compile group: 'log4j', name: 'log4j', version: '1.2.16'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile 'info.cukes:cucumber-java:1.2.4'
    testCompile 'info.cukes:cucumber-junit:1.2.4'
    // https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2
    testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '1.7.4'


// https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4
    testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.4'

// https://mvnrepository.com/artifact/org.mockito/mockito-core
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.9'




}


task cucumber() {
    dependsOn assemble, compileTestJava, jar
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'com.pixyfi.sdk.steps', 'src/test/resources']
        }
    }
}


test {
    testLogging {
        // Show that tests are run in the command-line output
        events 'started', 'passed'
        showStandardStreams = true
    }
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

我不知道为什么它不起作用。请让我知道是什么问题

0 个答案:

没有答案