Search in sources :

Example 1 with MockedStatic

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);
    }
}
Also used : Assert(org.assertj.core.api.Assert) InOrder(org.mockito.InOrder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.mockStatic(org.mockito.Mockito.mockStatic) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) VerificationInOrderFailure(org.mockito.exceptions.verification.VerificationInOrderFailure) MockitoException(org.mockito.exceptions.base.MockitoException) VerificationMode(org.mockito.verification.VerificationMode) Mockito.timeout(org.mockito.Mockito.timeout) MockedStatic(org.mockito.MockedStatic) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Mockito.inOrder(org.mockito.Mockito.inOrder) Assert.fail(org.junit.Assert.fail) NotAMockException(org.mockito.exceptions.misusing.NotAMockException) Mockito.mock(org.mockito.Mockito.mock) InOrder(org.mockito.InOrder) VerificationMode(org.mockito.verification.VerificationMode) Test(org.junit.Test)

Example 2 with MockedStatic

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());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Random(java.util.Random) HashSet(java.util.HashSet) AlluxioNodeType(alluxio.hub.proto.AlluxioNodeType) HubNodeState(alluxio.hub.proto.HubNodeState) GrpcChannel(alluxio.grpc.GrpcChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HubNodeAddress(alluxio.hub.proto.HubNodeAddress) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) HubTestUtils.generateNodeAddress(alluxio.hub.manager.util.HubTestUtils.generateNodeAddress) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) HubTestUtils.hubStatusToAlluxioStatus(alluxio.hub.manager.util.HubTestUtils.hubStatusToAlluxioStatus) AlluxioNodeStatus(alluxio.hub.proto.AlluxioNodeStatus) Executors(java.util.concurrent.Executors) BaseHubTest(alluxio.hub.test.BaseHubTest) Mockito(org.mockito.Mockito) GrpcChannelBuilder(alluxio.grpc.GrpcChannelBuilder) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) TestLoggerRule(alluxio.TestLoggerRule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HubNodeAddress(alluxio.hub.proto.HubNodeAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GrpcChannelBuilder(alluxio.grpc.GrpcChannelBuilder) GrpcChannel(alluxio.grpc.GrpcChannel) Test(org.junit.Test) BaseHubTest(alluxio.hub.test.BaseHubTest)

Example 3 with MockedStatic

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());
    }
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) IOException(java.io.IOException) OSUtils(alluxio.util.OSUtils) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockedStatic(org.mockito.MockedStatic) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Assume(org.junit.Assume) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert.assertEquals(org.junit.Assert.assertEquals) Stream(java.util.stream.Stream) IOException(java.io.IOException) Files(java.nio.file.Files) Test(org.junit.Test)

Example 4 with MockedStatic

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());
    }
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) IOException(java.io.IOException) OSUtils(alluxio.util.OSUtils) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockedStatic(org.mockito.MockedStatic) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) Assume(org.junit.Assume) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert.assertEquals(org.junit.Assert.assertEquals) Stream(java.util.stream.Stream) IOException(java.io.IOException) Files(java.nio.file.Files) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 MockedStatic (org.mockito.MockedStatic)4 Stream (java.util.stream.Stream)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Mockito (org.mockito.Mockito)3 OSUtils (alluxio.util.OSUtils)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers (org.hamcrest.Matchers)2 Assume (org.junit.Assume)2 ArgumentMatchers (org.mockito.ArgumentMatchers)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.times (org.mockito.Mockito.times)2 Answer (org.mockito.stubbing.Answer)2 TestLoggerRule (alluxio.TestLoggerRule)1 GrpcChannel (alluxio.grpc.GrpcChannel)1