Mockito Programming Model: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 26: Line 26:


=Annotations=
=Annotations=
<syntaxhighlight lang='java'>
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 {
    ...
  }
}
</syntaxhighlight>

Revision as of 04:03, 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 {
    ...
  }
}