use of org.mockito.MockedStatic in project mockito by mockito.
the class InOrderVerificationTest method shouldThrowExceptionWhenModeIsUnsupported.
@Test
public void shouldThrowExceptionWhenModeIsUnsupported() {
try (MockedStatic<StaticContext> mockedStatic = mockStatic(StaticContext.class)) {
// given
VerificationMode unsupportedMode = data -> {
};
InOrder inOrder = inOrder(StaticContext.class);
// when
StaticContext.firstMethod();
// then
assertThatThrownBy(() -> inOrder.verify(mockedStatic, StaticContext::firstMethod, unsupportedMode)).isInstanceOf(MockitoException.class);
}
}
use of org.mockito.MockedStatic in project alluxio by Alluxio.
the class HubClusterTest method testExecAllNodes.
@Test
public void testExecAllNodes() throws Exception {
List<HubNodeAddress> nodes = Stream.of(1, 2, 3, 4, 5).map(i -> generateNodeAddress()).collect(Collectors.toList());
nodes.forEach(mCluster::add);
try (MockedStatic<GrpcChannelBuilder> mock = Mockito.mockStatic(GrpcChannelBuilder.class)) {
GrpcChannelBuilder mockBuilder = mock(GrpcChannelBuilder.class);
doReturn(mockBuilder).when(mockBuilder).setSubject(any());
doReturn(mock(GrpcChannel.class)).when(mockBuilder).build();
mock.when(() -> GrpcChannelBuilder.newBuilder(any(), any())).thenReturn(mockBuilder);
AtomicInteger i = new AtomicInteger(0);
// Test node single exec
mCluster.exec(Collections.singleton(nodes.get(0)), getTestConfig(), (client) -> i.incrementAndGet(), mSvc);
assertEquals(1, i.get());
i.set(0);
// Test multiple node exec
mCluster.exec(new HashSet<>(nodes.subList(0, 3)), getTestConfig(), (client) -> i.incrementAndGet(), mSvc);
assertEquals(3, i.get());
i.set(0);
// Test 0 node exec
mCluster.exec(Collections.emptySet(), getTestConfig(), (client) -> i.incrementAndGet(), mSvc);
assertEquals(0, i.get());
i.set(0);
// test non existing node exec
mCluster.exec(Collections.singleton(generateNodeAddress()), getTestConfig(), (client) -> i.incrementAndGet(), mSvc);
assertEquals(0, i.get());
}
}
use of org.mockito.MockedStatic in project alluxio by Alluxio.
the class LinuxProcessTableTest method testReadBytesError.
@Test
public void testReadBytesError() throws IOException {
LinuxProcessTable procInfo = Mockito.spy(new LinuxProcessTable());
Mockito.doAnswer((Answer<Stream<Integer>>) (invocation) -> Stream.of(123)).when(procInfo).getAllPids();
try (MockedStatic<Files> fileMock = Mockito.mockStatic(Files.class)) {
fileMock.when(() -> Files.exists(ArgumentMatchers.any())).thenReturn(true);
fileMock.when(() -> Files.readAllBytes(ArgumentMatchers.eq(Paths.get("/proc", 123 + "", "comm")))).thenThrow(new IOException("expected"));
assertEquals(0, procInfo.getJavaPids().count());
}
}
use of org.mockito.MockedStatic in project alluxio by Alluxio.
the class LinuxProcessTableTest method testGetJavaPidReadBytesException.
@Test
public void testGetJavaPidReadBytesException() throws IOException {
LinuxProcessTable procInfo = Mockito.spy(new LinuxProcessTable());
Mockito.doAnswer((Answer<Stream<Integer>>) (inv) -> Stream.of(1234)).when(procInfo).getJavaPids();
try (MockedStatic<Files> fileMock = Mockito.mockStatic(Files.class)) {
fileMock.when(() -> Files.exists(ArgumentMatchers.any())).thenReturn(true);
fileMock.when(() -> Files.readAllBytes(ArgumentMatchers.any())).thenThrow(new IOException("expected"));
assertEquals(0, procInfo.getJavaPid(procInfo.getJavaPids(), NodeStatusTest.class).size());
}
}
Aggregations