How Static method mock in junit

Sanjay Singh
Jul 25, 2024

--

add dependency

  <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
 @Test
void given_request_when_approvalGroupUpload_then_exception_() throws Exception {
InputStream mockInputStream = new ByteArrayInputStream("sample data".getBytes());

Workbook mockWorkbook = mock(Workbook.class);
when(mockFile.isEmpty()).thenReturn(false);
when(mockFile.getInputStream()).thenReturn(mockInputStream);
Workbook workbook = Mockito.mock(Workbook.class);
MockedStatic<WorkbookFactory> classMock = mockStatic(WorkbookFactory.class);
classMock.when(() -> WorkbookFactory.create(mockInputStream)).thenReturn(mockWorkbook);

int[] response = service.approvalGroupUpload(mockFile);
assertEquals(false, response);
}

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

No responses yet