Mockito在我尝试运行Cucumber的StepDef时出现MissingMethodInvocationException。

huangapple 未分类评论77阅读模式
英文:

Mockito MissingMethodInvocationException when I try to run my Cucumber StepDef

问题

以下是翻译好的内容:

我试图在我的Cucumber步骤定义中使用Mockito模拟,但不知何故它无法正常工作。
每次我都会得到相同的org.mockito.exceptions.misusing.MissingMethodInvocationException错误。

代码很简单。

@Rule
public MockitoRule mockito = MockitoJUnit.rule();

@Mock
public PlayDeck mockDeck = new PlayDeck(6);

@Mock
public Hand hand = new Hand(this.mockDeck);

@Angenommen("ich habe eine Karo {int} auf der Hand mit dem Wert {int}")
public void ich_habe_eine_Karo_auf_der_Hand_mit_dem_Wert(final Integer int1, final Integer int2) {
  //given
  Mockito.when(this.mockDeck.drawCard())
      .thenReturn(Card.builder().color(CardColor.Diamond).face(getFaceByValue(int1)).build());
  //when
  this.hand.draw();
  //then
  Assertions.assertThat(this.hand.value()).isEqualTo(int2);
}

但每次我运行它时,它都会失败。文档说:when()需要一个参数,这个参数必须是'对模拟对象的方法调用'。但是我的when(...)确实包含一个方法。

我还尝试过使用BDDMockito

BDDMockito.given(this.mockDeck.drawCard())
    .willReturn(
        Card.builder().color(CardColor.Diamond).face(getFaceByValue(int1)).build());

相同的错误。但在我的单元测试中,它完全正常。例如在这里:

@Rule public MockitoRule mockito = MockitoJUnit.rule();

@Mock public PlayDeck mockDeck = new PlayDeck(8);

@Mock public Hand mockHand = new Hand(this.mockDeck);

@Test public void returnFalseIfBusted() {
    //given
    given(this.mockDeck.drawCard())
            .willReturn(Card.builder().color(CardColor.Heart).face(CardFace.KING).build());
    final Hand hand = new Hand(this.mockDeck);
    //when
    hand.draw();
    //then
    assertThat(hand.isBusted()).isTrue();
}

有人能帮帮我吗?谢谢!

英文:

I am trying to use Mockito mocks in my Cucumber Step Defs, but somehow it won't work.
I get the same org.mockito.exceptions.misusing.MissingMethodInvocationException every time.

The code is simple.

  @Rule
  public MockitoRule mockito = MockitoJUnit.rule();

  @Mock
  public PlayDeck mockDeck = new PlayDeck(6);

  @Mock
  public Hand hand = new Hand(this.mockDeck);

  @Angenommen("ich habe eine Karo {int} auf der Hand mit dem Wert {int}")
  public void ich_habe_eine_Karo_auf_der_Hand_mit_dem_Wert(final Integer int1, final Integer int2) {
    //given
    Mockito.when(this.mockDeck.drawCard())
        .thenReturn(Card.builder().color(CardColor.Diamond).face(getFaceByValue(int1)).build());
    //when
    this.hand.draw();
    //then
    Assertions.assertThat(this.hand.value()).isEqualTo(int2);
  }

But everytime I run it, it fails. The Documentation says: when() requires an argument which has to be 'a method call on a mock'. But my when(...) does have a Method inside.

I also tried to use BDDMockito

BDDMockito.given(this.mockDeck.drawCard())
        .willReturn(
            Card.builder().color(CardColor.Diamond).face(getFaceByValue(int1)).build());

Same error. But in my Unit tests, it works just fine. For example here:

@Rule public MockitoRule mockito = MockitoJUnit.rule();

@Mock public PlayDeck mockDeck = new PlayDeck(8);

@Mock public Hand mockHand = new Hand(this.mockDeck);

@Test public void returnFalseIfBusted() {
    //given
    given(this.mockDeck.drawCard())
            .willReturn(Card.builder().color(CardColor.Heart).face(CardFace.KING).build());
    final Hand hand = new Hand(this.mockDeck);
    //when
    hand.draw();
    //then
    assertThat(hand.isBusted()).isTrue();
}

Can someone help me out here? Thanks!

huangapple
  • 本文由 发表于 2020年8月14日 22:00:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/63414251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定