找到你要的答案

Q:PowerMockito UnfinishedStubbingException

Q:powermockito unfinishedstubbingexception

Hello I have this PowerMockito test which throws anì UnfinishedStubbingException

@RunWith(PowerMockRunner.class)
@PrepareForTest(SuperHero.class)
public class SMSActionPresaInCaricoTest {

@Test
public void testExecute() {
    PowerMockito.mockStatic(SuperHero.class);
    when(SuperHero.findSuperHero(anyString())).thenReturn(new SuperHero ());
    Action action = new Action();
    action.execute("", "");
}

The class SuperHero has a static method findSuperHero that returns an instance of SuperHero reading the data from DB.

I think the error is due to the fact that thenReturn is trying to return an instance of the mocked class SuperHero.

Is there a way to use PowerMockito with this scenario? Or is it some refactoring necessary?

你好,我有PowerMockito的测试,抛出一个ìunfinishedstubbingexception

@RunWith(PowerMockRunner.class)
@PrepareForTest(SuperHero.class)
public class SMSActionPresaInCaricoTest {

@Test
public void testExecute() {
    PowerMockito.mockStatic(SuperHero.class);
    when(SuperHero.findSuperHero(anyString())).thenReturn(new SuperHero ());
    Action action = new Action();
    action.execute("", "");
}

这类超级英雄都有一个静态方法findsuperhero返回英雄读取数据从数据库实例。

我认为错误是由于返回试图返回的嘲笑类超级英雄的一个实例。

有一种方法,使用PowerMockito这个场景呢?还是需要重构?

answer1: 回答1:

Sorry, cannot reproduce this one.

Here's my full test class, including all the imports:

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.junit.Assert.assertNotNull;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SuperHero.class)
public class SMSActionPresaInCaricoTest {

    @Test
    public void testExecute() {
        PowerMockito.mockStatic(SuperHero.class);
        when(SuperHero.findSuperHero(anyString())).thenReturn(new SuperHero());
        assertNotNull(SuperHero.findSuperHero("Batman"));
    }
}

When I ran this test, it passed.

I didn't have your SuperHero class, so I used this one instead:

package com.example;

public class SuperHero {
    public static SuperHero findSuperHero(String name) {
        throw new RuntimeException("This method should have been mocked");
    }
}

I'm using PowerMockito 1.6.2, in case that helps.

对不起,不能复制这个。

这是我的全部测试课,包括所有的进口:

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.junit.Assert.assertNotNull;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SuperHero.class)
public class SMSActionPresaInCaricoTest {

    @Test
    public void testExecute() {
        PowerMockito.mockStatic(SuperHero.class);
        when(SuperHero.findSuperHero(anyString())).thenReturn(new SuperHero());
        assertNotNull(SuperHero.findSuperHero("Batman"));
    }
}

当我运行这个测试,它通过。

我没有你的超级英雄类,所以我用这个吧:

package com.example;

public class SuperHero {
    public static SuperHero findSuperHero(String name) {
        throw new RuntimeException("This method should have been mocked");
    }
}

我用PowerMockito 1.6.2,以帮助。

java  mockito  powermock