Test
  • Introduction
  • Regression Testing
  • Unit Test
  • reflection
  • terminology
  • Defect Life Cycle
  • NullVSNotNull
  • @Before VS @BeforeEach VS...
  • JUnit Runner
  • run only functional test or integration test
  • cucumber/tags
  • cucumber/ options
  • cucumber/runwith
  • parallel cucumber test
  • cucumber/some eg
  • Hamcrest for testing
  • continuous integration
  • work flow for functional test
  • unit test for server side
  • other
  • Mock
  • jacoco&mutation
  • PowerMockito
  • performance test
Powered by GitBook
On this page
  • 1. Initialising Mock Objects - Mockito
  • 2. @Mock VS @InjectMocks

Was this helpful?

Mock

PreviousotherNextjacoco&mutation

Last updated 5 years ago

Was this helpful?

1. Initialising Mock Objects - Mockito

1). MockitoAnnotations

public class SampleBaseTestCase{

    @Before public void initMocks(){

        MockiotAnnotations.initMocks(this);
    }
}

2). RunWith

@RunWith(MockitoJUnitRunner.class)

3).

mock(xxx.class);

2. @Mock VS @InjectMocks

@Mock creates a mock. @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock(or @Spy) annotations into this instance. Note that you must use @RunWith(MockitoJUnitRunner.class) or Mockito.initMocks(this) to initialize these mocks and inject them.

Mockito.when(mockclass.methof(Mockito.any())).thenReturn(true);

https://stackoverflow.com/questions/15494926/initialising-mock-objects-mockito