How to use MockedStatic class of org.mockito package

Best Mockito code snippet using org.mockito.MockedStatic

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1package com.jun.services;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.ArgumentMatchers.eq;
5import static org.mockito.Mockito.doNothing;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.sql.Connection;
10import java.sql.SQLException;
11
12import org.junit.Before;
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.mockito.MockedStatic;
16import org.mockito.Mockito;
17
18import com.jun.dao.CustomerDAO;
19import com.jun.dao.LogDAO;
20import com.jun.dao.LoginDAO;
21import com.jun.exceptions.UserAlreadyExistsException;
22import com.jun.exceptions.UserNotFoundException;
23import com.jun.model.Login;
24import com.jun.util.ConnectionUtil;
25
26public class LoginServiceTest {
27
28	public LoginService loginService;
29	
30	public static LoginDAO loginDAO;
31	public static LogDAO logDAO;
32	public static CustomerDAO customerDAO;
33	public static Connection mockConnection;
34	public static MockedStatic<ConnectionUtil> mockedStatic;
35	
36	@BeforeClass
37	public static void setUpBeforeClass() throws Exception {
38		loginDAO = mock(LoginDAO.class);
39		logDAO = mock(LogDAO.class);
40		customerDAO = mock(CustomerDAO.class);
41		mockConnection = mock(Connection.class);
42		
43		when(loginDAO.authenticateUser(eq("user1"), eq("pw1"), eq(mockConnection))).thenReturn(new Login(false, 1));
44		when(loginDAO.createLogin(eq("jlei"), eq("pass"), eq("[email protected]"), eq(mockConnection))).thenReturn(3);
45		doNothing().when(logDAO).logUserAction(1, "test", mockConnection);
46		when(customerDAO.checkValidUnsername("jlei", mockConnection)).thenReturn(true);
47	}
48	
49	@Before 
50	public void setUp() throws Exception {
51		loginService = new LoginService(loginDAO, logDAO, customerDAO);
52	}
53	
54	@Test
55	public void testValidLogin() throws UserNotFoundException, SQLException {
56		try (MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
57			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
58			
59			Login actual = loginService.authenticateUser("user1", "pw1");
60			Login expected = new Login(false, 1);
61			
62			assertEquals(expected, actual);
63		}
64	}
65	
66	@Test(expected = UserNotFoundException.class)
67	public void testInvalidLogin() throws UserNotFoundException, SQLException {
68		try (MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
69			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
70			
71		loginService.authenticateUser("user2", "pw2");
72		}
73	}
74	
75	@Test
76	public void testCreateUser() throws UserAlreadyExistsException, SQLException {
77		try (MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
78			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
79			
80			boolean userCreated = loginService.createUser("jlei", "pass", "Jun", "Lei", "[email protected]", false);
81			boolean expected = true;
82			assertEquals(expected, userCreated);
83		}
84	}
85	
86	
87}
88
Full Screen
copy
1package com.jun.services;
2
3
4import static org.mockito.ArgumentMatchers.eq;
5import static org.mockito.Mockito.doNothing;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.sql.Connection;
10import java.sql.SQLException;
11
12import org.junit.Before;
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.mockito.MockedStatic;
16import org.mockito.Mockito;
17
18import com.jun.dao.CustomerDAO;
19import com.jun.dao.EmployeeDAO;
20import com.jun.dao.LogDAO;
21import com.jun.exceptions.ApplicationNotFoundException;
22import com.jun.util.ConnectionUtil;
23
24public class EmployeeServiceTest {
25	
26	public EmployeeService employeeService;
27	
28	public static LogDAO logDAO;
29	public static EmployeeDAO employeeDAO;
30	public static CustomerDAO customerDAO;
31	public static Connection mockConnection;
32	public static MockedStatic<ConnectionUtil> mockedStatic;
33	
34	@BeforeClass
35	public static void setUpBeforeClass() throws Exception {
36		logDAO = mock(LogDAO.class);
37		customerDAO = mock(CustomerDAO.class);
38		employeeDAO = mock(EmployeeDAO.class);
39		mockConnection = mock(Connection.class);
40		
41		doNothing().when(logDAO).logUserAction(1, "test", mockConnection);
42		when(employeeDAO.rejectAccount(eq(1), eq(mockConnection))).thenReturn(true);
43
44		when(employeeDAO.reviewCustomerApplication(eq(mockConnection))).thenReturn(null);
45	}
46	
47	@Before
48	public void setUp() throws Exception {
49		employeeService = new EmployeeService(logDAO, employeeDAO, customerDAO);
50	}
51	
52	@Test
53	public void testRejectApplication() throws SQLException {
54		try (MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
55			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
56			employeeService.rejectAccount(1);
57		}
58	}
59	
60	@Test(expected = ApplicationNotFoundException.class)
61	public void testApplicationNotFound() throws ApplicationNotFoundException, SQLException {
62		try (MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
63			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
64			employeeService.reviewApplications();
65		}
66	}
67	
68}
69
Full Screen
copy
1package com.jun.services;
2
3
4import static org.junit.Assert.assertEquals;
5import static org.mockito.ArgumentMatchers.eq;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.sql.Connection;
10import java.sql.SQLException;
11
12import org.junit.After;
13import org.junit.AfterClass;
14import org.junit.Before;
15import org.junit.BeforeClass;
16import org.junit.Test;
17import org.mockito.MockedStatic;
18import org.mockito.Mockito;
19
20import com.jun.dao.AccountDAO;
21import com.jun.exceptions.AccountNotFoundException;
22import com.jun.model.Account;
23import com.jun.util.ConnectionUtil;
24
25public class AccountServiceTest {
26
27	public AccountService accountService;
28	
29	public static AccountDAO accountDAO;
30	public static Connection mockConnection;
31	public static MockedStatic<ConnectionUtil> mockedStatic;
32	
33	@BeforeClass
34	public static void setUpBeforeClass() throws Exception {
35		accountDAO = mock(AccountDAO.class);
36		mockConnection = mock(Connection.class);
37		
38		when(accountDAO.getAccountInfo(eq("5432123456789876"), eq(mockConnection))).thenReturn(new Account("5432123456789876", 1000.00, 1, true));
39		
40	}
41	
42	@AfterClass
43	public static void tearDownAfterClass() throws Exception {
44	}
45	
46	@Before
47	public void setUp() throws Exception {
48		accountService = new AccountService(accountDAO);
49	}
50	
51	@After
52	public void tearDown() throws Exception {
53	}
54	
55	@Test
56	public void testValidAccount() throws AccountNotFoundException, SQLException {
57		try (MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
58			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
59			
60			Account actual = accountService.getAccountInfo("5432123456789876", 1);
61			
62			Account expected = new Account("5432123456789876", 1000.00, 1, true);
63			
64			assertEquals(expected, actual);
65		}
66	}
67	
68	@Test(expected = AccountNotFoundException.class)
69	public void testInvalidAccount() throws AccountNotFoundException, SQLException {
70		try(MockedStatic<ConnectionUtil> mockedStatic = Mockito.mockStatic(ConnectionUtil.class)) {
71			mockedStatic.when(ConnectionUtil::getConnection).thenReturn(mockConnection);
72			
73			accountService.getAccountInfo("5432123456789873", 2);
74		}
75	}
76	
77}
78
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 MockedStatic

    No relevant questions found for this class 😞

Most used methods in MockedStatic

    No methods found for this class 😞

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)