How to use Timer class of org.mockito.internal.util package

Best Mockito code snippet using org.mockito.internal.util.Timer

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*******************************************************************************
2 *                                                                              
3 *  Copyright FUJITSU LIMITED 2017
4 *                                                                              
5 *  Author: weiser                                                     
6 *                                                                              
7 *  Creation Date: 08.02.2011                                                      
8 *                                                                              
9 *  Completion Time: 08.02.2011                                              
10 *                                                                              
11 *******************************************************************************/
12
13package org.oscm.ui.beans.operator;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertFalse;
17import static org.junit.Assert.assertTrue;
18import static org.mockito.Mockito.doReturn;
19import static org.mockito.Mockito.doThrow;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.spy;
22
23import java.util.ArrayList;
24import java.util.Date;
25import java.util.List;
26import java.util.Locale;
27
28import javax.faces.application.FacesMessage;
29
30import org.junit.Before;
31import org.junit.Test;
32
33import org.oscm.test.stubs.OperatorServiceStub;
34import org.oscm.types.enumtypes.TimerType;
35import org.oscm.ui.beans.BaseBean;
36import org.oscm.ui.stubs.FacesContextStub;
37import org.oscm.internal.intf.OperatorService;
38import org.oscm.internal.types.exception.ValidationException;
39import org.oscm.internal.vo.VOTimerInfo;
40
41/**
42 * @author weiser
43 * 
44 */
45public class TimerBeanTest {
46
47    private TimerBean bean;
48    private TimerBean timerBean;
49    private OperatorService operatorService;
50
51    protected String messageKey;
52    protected boolean serviceCalled;
53    protected List<VOTimerInfo> expirationInfo;
54
55    private final List<FacesMessage> facesMessages = new ArrayList<FacesMessage>();
56
57    @Before
58    public void setup() {
59        timerBean = spy(new TimerBean());
60        operatorService = mock(OperatorService.class);
61        doReturn(operatorService).when(timerBean).getOperatorService();
62
63        new FacesContextStub(Locale.ENGLISH) {
64            public void addMessage(String arg0, FacesMessage arg1) {
65                facesMessages.add(arg1);
66            }
67        };
68
69        final OperatorServiceStub stub = new OperatorServiceStub() {
70
71            public List<VOTimerInfo> getTimerExpirationInformation() {
72                serviceCalled = true;
73                return expirationInfo;
74            }
75
76            public List<VOTimerInfo> reInitTimers() {
77                serviceCalled = true;
78                return expirationInfo;
79            }
80
81        };
82        bean = new TimerBean() {
83
84            private static final long serialVersionUID = 5713210290878291988L;
85
86            protected OperatorService getOperatorService() {
87                return stub;
88            }
89
90            protected void addMessage(final String clientId,
91                    final FacesMessage.Severity severity, final String key) {
92                messageKey = key;
93            }
94
95        };
96        expirationInfo = new ArrayList<VOTimerInfo>();
97        VOTimerInfo o = new VOTimerInfo();
98        o.setExpirationDate(new Date());
99        o.setTimerType(TimerType.BILLING_INVOCATION.name());
100        expirationInfo.add(o);
101    }
102
103    @Test
104    public void testGetExpirationInfo() throws Exception {
105        List<VOTimerInfo> list = bean.getExpirationInfo();
106        assertEquals(expirationInfo, list);
107        assertTrue(serviceCalled);
108        // the second call must do no service call because the data is cached
109        // but return the same data
110        serviceCalled = false;
111        list = bean.getExpirationInfo();
112        assertEquals(expirationInfo, list);
113        assertFalse(serviceCalled);
114    }
115
116    @Test
117    public void testReInitTimers() throws Exception {
118        String result = bean.reInitTimers();
119        assertEquals(BaseBean.OUTCOME_SUCCESS, result);
120        assertEquals(BaseOperatorBean.INFO_TASK_SUCCESSFUL, messageKey);
121        assertTrue(serviceCalled);
122        // reinit timers also returns the timer info - so the field must be not
123        // null and thus no service must be called to get the timer information
124        serviceCalled = false;
125        List<VOTimerInfo> list = bean.getExpirationInfo();
126        assertEquals(expirationInfo, list);
127        assertFalse(serviceCalled);
128    }
129
130    @Test
131    public void testReInitTimers_timerIntervalInvalid() throws Exception {
132        // given
133        doThrow(new ValidationException()).when(operatorService).reInitTimers();
134
135        // when
136        String result = timerBean.reInitTimers();
137
138        // then
139        assertEquals(BaseBean.OUTCOME_ERROR, result);
140    }
141}
142
Full Screen
copy
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.flink.runtime.taskexecutor.slot;
20
21import org.apache.flink.runtime.clusterframework.types.AllocationID;
22import org.apache.flink.util.TestLogger;
23
24import org.junit.Test;
25import org.mockito.internal.util.reflection.Whitebox;
26
27import java.util.Map;
28import java.util.concurrent.ScheduledExecutorService;
29import java.util.concurrent.ScheduledFuture;
30import java.util.concurrent.TimeUnit;
31
32import static org.junit.Assert.*;
33import static org.mockito.Matchers.any;
34import static org.mockito.Matchers.anyLong;
35import static org.mockito.Mockito.mock;
36import static org.mockito.Mockito.times;
37import static org.mockito.Mockito.verify;
38import static org.mockito.Mockito.when;
39
40public class TimerServiceTest extends TestLogger {
41	/**
42	 * Test all timeouts registered can be unregistered
43	 * @throws Exception
44   */
45	@Test
46	@SuppressWarnings("unchecked")
47	public void testUnregisterAllTimeouts() throws Exception {
48		// Prepare all instances.
49		ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
50		ScheduledFuture scheduledFuture = mock(ScheduledFuture.class);
51		when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)))
52			.thenReturn(scheduledFuture);
53		TimerService<AllocationID> timerService = new TimerService<>(scheduledExecutorService, 100L);
54		TimeoutListener<AllocationID> listener = mock(TimeoutListener.class);
55
56		timerService.start(listener);
57
58		// Invoke register and unregister.
59		timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
60		timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
61
62		timerService.unregisterAllTimeouts();
63
64		// Verify.
65		Map<?, ?> timeouts = (Map<?, ?>) Whitebox.getInternalState(timerService, "timeouts");
66		assertTrue(timeouts.isEmpty());
67		verify(scheduledFuture, times(2)).cancel(true);
68	}
69
70}
71
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on Timer

    No relevant questions found for this class 😞

Most used methods in Timer

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)