Spock Power Mock不返回.thenAnswer {}方法中指定的值

问题描述 投票:0回答:1

我有一个具有public static void方法的类。此方法使用不同类中的方法,并且该方法采用两个参数并返回List值。

public class FirstClass {
    public static void doSomething() {
        List<String> values = SecondClass.getOrderedValuesBy(anotherMethod());
        // action 
    }

    private static String anotherMethod() {
        // some action
        return ""; // something that depends on action above.
    }
}

public class SecondClass {
    public static List<String> getOrderedValuesBy(String by) {
        List<String> values = new ArrayList<>();
        values.addAll(getOrderedValuesBy(by, true));
        return values;
    }

    private static List<String> getOrderedValuesBy(String by, boolean ordered) {
       // some action
    }
}

然后编写一些测试用的代码时,我在mockStatic上使用SecondClass方法,并将FirstClass初始化为新对象,如下所示。

def "someTest"() {
    given:
    mockStatic(SecondClass)
    FirstClass firstClass = new FirstClass()
    when(SecondClass.getOrderedValuesBy(anyString()).thenAnswer {['some values'])}
}

它不返回我想拥有的这些值,因为我需要在doSomething() void方法中调用的某些私有方法中使用它们,并且这些参数作为values列表作为参数。有人对我做错了什么有什么想法吗,以及如何操作在FirstClass

中的void方法中调用的此方法返回的值?
java groovy spock powermock
1个回答
0
投票

您可以同时使用thenReturnthenAnswer,这取决于您在返回值之前要执行的操作,如@ Jordan所述。这是一个MCVE

应用程序类:

package de.scrum_master.stackoverflow.q59993907;

import java.util.ArrayList;
import java.util.List;

public class SecondClass {
  public static List<String> getOrderedValuesBy(String by) {
    List<String> values = new ArrayList<>();
    values.addAll(getOrderedValuesBy(by, true));
    return values;
  }

  private static List<String> getOrderedValuesBy(String by, boolean ordered) {
    // some action
    List<String> result = new ArrayList<>();
    result.add("one");
    result.add("two");
    result.add("three");
    return result;
  }
}
package de.scrum_master.stackoverflow.q59993907;

import java.util.List;

public class FirstClass {
  public static void doSomething() {
    List<String> values = SecondClass.getOrderedValuesBy(anotherMethod());
    // action
    System.out.println(values);
  }

  private static String anotherMethod() {
    // some action
    return ""; // something that depends on action above.
  }
}

使用PowerMockito的Spock规范:

package de.scrum_master.stackoverflow.q59993907

import org.junit.runner.RunWith
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.modules.junit4.PowerMockRunnerDelegate
import org.spockframework.runtime.Sputnik
import spock.lang.Specification

import static de.scrum_master.stackoverflow.q59993907.SecondClass.getOrderedValuesBy
import static org.mockito.ArgumentMatchers.anyString
import static org.powermock.api.mockito.PowerMockito.mockStatic
import static org.powermock.api.mockito.PowerMockito.when

@RunWith(PowerMockRunner)
@PowerMockRunnerDelegate(Sputnik)
@PrepareForTest(SecondClass)
class PowerMockStaticTest extends Specification {
  static final PrintStream originalSysOut = System.out
  PrintStream mockSysOut = Mock()

  def setup() {
    System.out = mockSysOut
  }

  def cleanup() {
    System.out = originalSysOut
  }

  def "no mock"() {
    when:
    FirstClass.doSomething()

    then:
    1 * mockSysOut.println(['one', 'two', 'three'])
  }

  def "when-thenAnswer"() {
    given:
    mockStatic(SecondClass)
    when(getOrderedValuesBy(anyString())).thenAnswer { ['thenAnswer'] }

    when:
    FirstClass.doSomething()

    then:
    1 * mockSysOut.println(['thenAnswer'])
  }

  def "when-thenReturn"() {
    given:
    mockStatic(SecondClass)
    when(getOrderedValuesBy(anyString())).thenReturn(['thenReturn'])

    when:
    FirstClass.doSomething()

    then:
    1 * mockSysOut.println(['thenReturn'])
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.