Mockito Programming Model: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 56: Line 56:
=Stub Functionality=
=Stub Functionality=
A [[Software_Testing_Concepts#Stub|stub]] returns synthetic responses or throws exceptions when its methods are invoked. Mockito supports stubbing and by returning a given value when a specific method of the [[Mockito_Concepts#Managed_Test_Double|managed test double]] is called. Mockito implements the stub functionality with <code>Mockito.when(<method>).thenReturn(...)</code> pattern:
A [[Software_Testing_Concepts#Stub|stub]] returns synthetic responses or throws exceptions when its methods are invoked. Mockito supports stubbing and by returning a given value when a specific method of the [[Mockito_Concepts#Managed_Test_Double|managed test double]] is called. Mockito implements the stub functionality with <code>Mockito.when(<method>).thenReturn(...)</code> pattern:
<syntaxhighlight lang='java'>
public class ExternalDependency {
  public String someMethod() {
    ...
  }
}
</syntaxhighlight>
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
public class SomeClassTest {
public class SomeClassTest {
Line 67: Line 74:
     ...
     ...
   }
   }
}
</syntaxhighlight>
==Argument Matcher==
<syntaxhighlight lang='java'>
public class ExternalDependency {
  public String someMethodWithArg(String s) {
    ...
  }
}
</syntaxhighlight>
<syntaxhighlight lang='java'>
import static org.mockito.ArgumentMatchers.isA;
  ...
  Mockito.when(mockExternalDependency.someMethodWithArg(isA(String.class))).thenReturn(...);
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 04:40, 16 July 2021

Internal

Overview

API

import static org.mockito.Mockito.mock;
import com.example.ExternalDependency;

public class SomeClassTest {

  private ExternalDependency mockExternalDependency;

  @Before
  public void setUp() {
    mockExternalDependency = mock(ExternalDependency.class);
  }

  @Test
  public void someTest() throws Exception {
    ...
  }
}

Annotations

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.example.ExternalDependency;

public class SomeClassTest {

  private AutoCloseable mocks;

  @Mock
  private ExternalDependency mockExternalDependency;

  @Before
  public void openMocks() {
    mocks = MockitoAnnotations.openMocks(this);
  }

  @After
  public void releaseMocks() throws Exception {
    mocks.close();
  }

  @Test
  public void someTest() throws Exception {
    ...
  }
}

Stub Functionality

A stub returns synthetic responses or throws exceptions when its methods are invoked. Mockito supports stubbing and by returning a given value when a specific method of the managed test double is called. Mockito implements the stub functionality with Mockito.when(<method>).thenReturn(...) pattern:

public class ExternalDependency {
  public String someMethod() {
    ...
  }
}
public class SomeClassTest {

  @Mock
  private ExternalDependency mockExternalDependency;

  @Test
  public void someTest() throws Exception {
    Mockito.when(mockExternalDependency.someMethod()).thenReturn("some synthetic result");
    ...
  }
}

Argument Matcher

public class ExternalDependency {
  public String someMethodWithArg(String s) {
    ...
  }
}
import static org.mockito.ArgumentMatchers.isA;
  ...
  Mockito.when(mockExternalDependency.someMethodWithArg(isA(String.class))).thenReturn(...);
}