How to use isNiceReplayAndVerifyMode method of org.powermock.api.easymock.PowerMock class

Best Powermock code snippet using org.powermock.api.easymock.PowerMock.isNiceReplayAndVerifyMode

Source:PowerMock.java Github

copy

Full Screen

...1447 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1448 if (invocationControl != null) {1449 invocationControl.reset();1450 } else {1451 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1452 // ignore non-mock1453 } else {1454 /*1455 * Delegate to easy mock class extension if we have1456 * no handler registered for this object.1457 */1458 try {1459 org.easymock.EasyMock.reset(mock);1460 } catch (RuntimeException e) {1461 throw new RuntimeException(mock + " is not a mock object", e);1462 }1463 }1464 }1465 }1466 }1467 } catch (Throwable t) {1468 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false);1469 if (t instanceof RuntimeException) {1470 throw (RuntimeException) t;1471 } else if (t instanceof Error) {1472 throw (Error) t;1473 }1474 throw new RuntimeException(t);1475 }1476 }1477 /**1478 * Verify all classes and mock objects known by PowerMock. This includes all1479 * classes that are prepared for test using the {@link PrepareForTest} or1480 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1481 * their static initializers removed by using the1482 * {@link SuppressStaticInitializationFor} annotation. It also includes all1483 * mock instances created by PowerMock such as those created or used by1484 * {@link #createMock(Class, Method...)},1485 * {@link #mockStatic(Class, Method...)},1486 * {@link #expectNew(Class, Object...)},1487 * {@link #createPartialMock(Class, String...)} etc.1488 * <p>1489 * Note that all <tt>additionalMocks</tt> passed to the1490 * {@link #replayAll(Object...)} method are also verified here1491 * automatically.1492 *1493 */1494 public static synchronized void verifyAll() {1495 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1496 verify(classToReplayOrVerify);1497 }1498 }1499 /**1500 * Switches the mocks or classes to replay mode. Note that you must use this1501 * method when using PowerMock!1502 *1503 * @param mocks1504 * mock objects or classes loaded by PowerMock.1505 * @throws Exception1506 * If something unexpected goes wrong.1507 */1508 public static synchronized void replay(Object... mocks) {1509 try {1510 for (Object mock : mocks) {1511 if (mock instanceof Class<?>) {1512 replay((Class<?>) mock);1513 } else {1514 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1515 if (invocationControl != null) {1516 invocationControl.replay();1517 } else {1518 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1519 // ignore non-mock1520 } else {1521 /*1522 * Delegate to easy mock class extension if we have1523 * no handler registered for this object.1524 */1525 try {1526 org.easymock.EasyMock.replay(mock);1527 } catch (RuntimeException e) {1528 throw new RuntimeException(mock + " is not a mock object", e);1529 }1530 }1531 }1532 }1533 }1534 } catch (Throwable t) {1535 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false);1536 if (t instanceof RuntimeException) {1537 throw (RuntimeException) t;1538 } else if (t instanceof Error) {1539 throw (Error) t;1540 }1541 throw new RuntimeException(t);1542 }1543 }1544 /**1545 * Switches the mocks or classes to verify mode. Note that you must use this1546 * method when using PowerMock!1547 *1548 * @param objects1549 * mock objects or classes loaded by PowerMock.1550 */1551 public static synchronized void verify(Object... objects) {1552 for (Object mock : objects) {1553 if (mock instanceof Class<?>) {1554 verifyClass((Class<?>) mock);1555 } else {1556 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1557 if (invocationControl != null) {1558 invocationControl.verify();1559 } else {1560 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1561 // ignore non-mock1562 } else {1563 /*1564 * Delegate to easy mock class extension if we have no1565 * handler registered for this object.1566 */1567 try {1568 org.easymock.EasyMock.verify(mock);1569 } catch (RuntimeException e) {1570 throw new RuntimeException(mock + " is not a mock object", e);1571 }1572 }1573 }1574 }1575 }1576 }1577 /**1578 * Convenience method for createMock followed by expectNew.1579 *1580 * @param type1581 * The class that should be mocked.1582 * @param arguments1583 * The constructor arguments.1584 * @return A mock object of the same type as the mock.1585 * @throws Exception1586 */1587 public static synchronized <T> T createMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1588 T mock = createMock(type);1589 expectNew(type, arguments).andReturn(mock);1590 return mock;1591 }1592 /**1593 * Convenience method for createMock followed by expectNew when PowerMock1594 * cannot determine which constructor to use automatically. This happens1595 * when you have one constructor taking a primitive type and another one1596 * taking the wrapper type of the primitive. For example <code>int</code>1597 * and <code>Integer</code>.1598 *1599 * @param type1600 * The class that should be mocked.1601 * @param parameterTypes1602 * The constructor parameter types.1603 * @param arguments1604 * The constructor arguments.1605 * @return A mock object of the same type as the mock.1606 * @throws Exception1607 */1608 public static synchronized <T> T createMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1609 Object... arguments) throws Exception {1610 T mock = createMock(type);1611 expectNew(type, parameterTypes, arguments).andReturn(mock);1612 return mock;1613 }1614 /**1615 * Convenience method for createNiceMock followed by expectNew.1616 *1617 * @param type1618 * The class that should be mocked.1619 * @param arguments1620 * The constructor arguments.1621 * @return A mock object of the same type as the mock.1622 * @throws Exception1623 */1624 public static synchronized <T> T createNiceMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1625 T mock = createNiceMock(type);1626 IExpectationSetters<T> expectationSetters = expectNiceNew(type, arguments);1627 if (expectationSetters != null) {1628 expectationSetters.andReturn(mock);1629 }1630 return mock;1631 }1632 /**1633 * Convenience method for createNiceMock followed by expectNew when1634 * PowerMock cannot determine which constructor to use automatically. This1635 * happens when you have one constructor taking a primitive type and another1636 * one taking the wrapper type of the primitive. For example1637 * <code>int</code> and <code>Integer</code>.1638 *1639 * @param type1640 * The class that should be mocked.1641 * @param parameterTypes1642 * The constructor parameter types.1643 * @param arguments1644 * The constructor arguments.1645 * @return A mock object of the same type as the mock.1646 * @throws Exception1647 */1648 public static synchronized <T> T createNiceMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1649 Object... arguments) throws Exception {1650 T mock = createNiceMock(type);1651 IExpectationSetters<T> expectationSetters = expectNiceNew(type, parameterTypes, arguments);1652 if (expectationSetters != null) {1653 expectationSetters.andReturn(mock);1654 }1655 return mock;1656 }1657 /**1658 * Convenience method for createStrictMock followed by expectNew.1659 *1660 * @param type1661 * The class that should be mocked.1662 * @param arguments1663 * The constructor arguments.1664 * @return A mock object of the same type as the mock.1665 * @throws Exception1666 */1667 public static synchronized <T> T createStrictMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1668 T mock = createStrictMock(type);1669 expectStrictNew(type, arguments).andReturn(mock);1670 return mock;1671 }1672 /**1673 * Convenience method for createStrictMock followed by expectNew when1674 * PowerMock cannot determine which constructor to use automatically. This1675 * happens when you have one constructor taking a primitive type and another1676 * one taking the wrapper type of the primitive. For example1677 * <code>int</code> and <code>Integer</code>.1678 *1679 * @param type1680 * The class that should be mocked.1681 * @param parameterTypes1682 * The constructor parameter types.1683 * @param arguments1684 * The constructor arguments.1685 * @return A mock object of the same type as the mock.1686 * @throws Exception1687 */1688 public static synchronized <T> T createStrictMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1689 Object... arguments) throws Exception {1690 T mock = createStrictMock(type);1691 expectStrictNew(type, parameterTypes, arguments).andReturn(mock);1692 return mock;1693 }1694 /**1695 * Allows specifying expectations on new invocations. For example you might1696 * want to throw an exception or return a mock. Note that you must replay1697 * the class when using this method since this behavior is part of the class1698 * mock.1699 * <p>1700 * Use this method when you need to specify parameter types for the1701 * constructor when PowerMock cannot determine which constructor to use1702 * automatically. In most cases you should use1703 * {@link #expectNew(Class, Object...)} instead.1704 */1705 public static synchronized <T> IExpectationSetters<T> expectNew(Class<T> type, Class<?>[] parameterTypes,1706 Object... arguments) throws Exception {1707 return doExpectNew(type, new DefaultMockStrategy(), parameterTypes, arguments);1708 }1709 @SuppressWarnings("unchecked")1710 private static <T> IExpectationSetters<T> doExpectNew(Class<T> type, MockStrategy mockStrategy,1711 Class<?>[] parameterTypes, Object... arguments) throws Exception {1712 if (type == null) {1713 throw new IllegalArgumentException("type cannot be null");1714 } else if (mockStrategy == null) {1715 throw new IllegalArgumentException("Internal error: Mock strategy cannot be null");1716 }1717 final boolean isNiceMock = mockStrategy instanceof NiceMockStrategy;1718 final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getUnmockedType(type);1719 if (!isNiceMock) {1720 if (parameterTypes == null) {1721 WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments);1722 } else {1723 WhiteboxImpl.getConstructor(unmockedType, parameterTypes);1724 }1725 }1726 /*1727 * Check if this type has been mocked before1728 */1729 NewInvocationControl<IExpectationSetters<T>> newInvocationControl = (NewInvocationControl<IExpectationSetters<T>>) MockRepository1730 .getNewInstanceControl(unmockedType);1731 if (newInvocationControl == null) {1732 InvocationSubstitute<T> mock = doMock(InvocationSubstitute.class, false, mockStrategy, null,1733 (Method[]) null);1734 newInvocationControl = new NewInvocationControlImpl<T>(mock, type);1735 MockRepository.putNewInstanceControl(type, newInvocationControl);1736 MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getUnmockedType(type));1737 }1738 if (isNiceMock && (arguments == null || arguments.length == 0)) {1739 return null;1740 }1741 return newInvocationControl.expectSubstitutionLogic(arguments);1742 }1743 /**1744 * Allows specifying expectations on new invocations. For example you might1745 * want to throw an exception or return a mock. Note that you must replay1746 * the class when using this method since this behavior is part of the class1747 * mock.1748 */1749 public static synchronized <T> IExpectationSetters<T> expectNew(Class<T> type, Object... arguments)1750 throws Exception {1751 return doExpectNew(type, new DefaultMockStrategy(), null, arguments);1752 }1753 /**1754 * Allows specifying expectations on new invocations for private member1755 * (inner) classes, local or anonymous classes. For example you might want1756 * to throw an exception or return a mock. Note that you must replay the1757 * class when using this method since this behavior is part of the class1758 * mock.1759 *1760 * @param fullyQualifiedName1761 * The fully-qualified name of the inner/local/anonymous type to1762 * expect.1763 * @param arguments1764 * Optional number of arguments.1765 */1766 @SuppressWarnings("unchecked")1767 public static synchronized <T> IExpectationSetters<T> expectNew(String fullyQualifiedName, Object... arguments)1768 throws Exception {1769 final Class<?> forName = Class.forName(fullyQualifiedName);1770 return (IExpectationSetters<T>) doExpectNew(forName, new DefaultMockStrategy(), null, arguments);1771 }1772 /**1773 * Allows specifying expectations on new invocations. For example you might1774 * want to throw an exception or return a mock.1775 * <p>1776 * This method checks the order of constructor invocations.1777 * <p>1778 * Note that you must replay the class when using this method since this1779 * behavior is part of the class mock.1780 */1781 public static synchronized <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Object... arguments)1782 throws Exception {1783 return doExpectNew(type, new StrictMockStrategy(), null, arguments);1784 }1785 /**1786 * Allows specifying expectations on new invocations. For example you might1787 * want to throw an exception or return a mock. Note that you must replay1788 * the class when using this method since this behavior is part of the class1789 * mock.1790 * <p>1791 * This method checks the order of constructor invocations.1792 * <p>1793 * Use this method when you need to specify parameter types for the1794 * constructor when PowerMock cannot determine which constructor to use1795 * automatically. In most cases you should use1796 * {@link #expectNew(Class, Object...)} instead.1797 */1798 public static synchronized <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Class<?>[] parameterTypes,1799 Object... arguments) throws Exception {1800 return doExpectNew(type, new StrictMockStrategy(), parameterTypes, arguments);1801 }1802 /**1803 * Allows specifying expectations on new invocations. For example you might1804 * want to throw an exception or return a mock.1805 * <p>1806 * This method allows any number of calls to a new constructor without1807 * throwing an exception.1808 * <p>1809 * Note that you must replay the class when using this method since this1810 * behavior is part of the class mock.1811 */1812 public static synchronized <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Object... arguments)1813 throws Exception {1814 return doExpectNew(type, new NiceMockStrategy(), null, arguments);1815 }1816 /**1817 * Allows specifying expectations on new invocations. For example you might1818 * want to throw an exception or return a mock. Note that you must replay1819 * the class when using this method since this behavior is part of the class1820 * mock.1821 * <p>1822 * This method allows any number of calls to a new constructor without1823 * throwing an exception.1824 * <p>1825 * Use this method when you need to specify parameter types for the1826 * constructor when PowerMock cannot determine which constructor to use1827 * automatically. In most cases you should use1828 * {@link #expectNew(Class, Object...)} instead.1829 */1830 public static synchronized <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Class<?>[] parameterTypes,1831 Object... arguments) throws Exception {1832 return doExpectNew(type, new NiceMockStrategy(), parameterTypes, arguments);1833 }1834 /**1835 * Suppress constructor calls on specific constructors only.1836 *1837 * @deprecated Use {@link #suppress(Constructor[])} instead.1838 */1839 public static synchronized void suppressConstructor(Constructor<?>... constructors) {1840 SuppressCode.suppressConstructor(constructors);1841 }1842 /**1843 * This method can be used to suppress the code in a specific constructor.1844 *1845 * @param clazz1846 * The class where the constructor is located.1847 * @param parameterTypes1848 * The parameter types of the constructor to suppress.1849 * @deprecated Use {@link #suppress(Constructor)} instead.1850 */1851 public static synchronized void suppressSpecificConstructor(Class<?> clazz, Class<?>... parameterTypes) {1852 SuppressCode.suppressSpecificConstructor(clazz, parameterTypes);1853 }1854 /**1855 * Suppress all constructors in the given class and it's super classes.1856 *1857 * @param classes1858 * The classes whose constructors will be suppressed.1859 * @deprecated Use {@link #suppress(Constructor[])} instead.1860 */1861 public static synchronized void suppressConstructor(Class<?>... classes) {1862 SuppressCode.suppressConstructor(classes);1863 }1864 /**1865 * Suppress all constructors in the given class.1866 *1867 * @param clazz1868 * The classes whose constructors will be suppressed.1869 * @param excludePrivateConstructors1870 * optionally keep code in private constructors1871 * @deprecated Use {@link #suppress(Constructor[])} instead.1872 */1873 public static synchronized void suppressConstructor(Class<?> clazz, boolean excludePrivateConstructors) {1874 SuppressCode.suppressConstructor(clazz, excludePrivateConstructors);1875 }1876 /**1877 * Suppress specific fields. This works on both instance methods and static1878 * methods. Note that replay and verify are not needed as this is not part1879 * of a mock behavior.1880 *1881 * @deprecated Use {@link #suppress(Field[])} instead.1882 */1883 public static synchronized void suppressField(Field... fields) {1884 SuppressCode.suppressField(fields);1885 }1886 /**1887 * Suppress all fields for these classes.1888 *1889 * @deprecated Use {@link #suppress(Field[])} instead.1890 */1891 public static synchronized void suppressField(Class<?>[] classes) {1892 SuppressCode.suppressField(classes);1893 }1894 /**1895 * Suppress multiple methods for a class.1896 *1897 * @param clazz1898 * The class whose methods will be suppressed.1899 * @param fieldNames1900 * The names of the methods that'll be suppressed. If field names1901 * are empty, <i>all</i> fields in the supplied class will be1902 * suppressed.1903 * @deprecated Use {@link #suppress(Field)} instead.1904 */1905 public static synchronized void suppressField(Class<?> clazz, String... fieldNames) {1906 SuppressCode.suppressField(clazz, fieldNames);1907 }1908 /**1909 * Suppress specific method calls on all types containing this method. This1910 * works on both instance methods and static methods. Note that replay and1911 * verify are not needed as this is not part of a mock behavior.1912 *1913 * @deprecated Use {@link #suppress(Method[])} instead.1914 */1915 public static synchronized void suppressMethod(Method... methods) {1916 SuppressCode.suppressMethod(methods);1917 }1918 /**1919 * Suppress all methods for these classes.1920 *1921 * @param cls1922 * The first class whose methods will be suppressed.1923 * @param additionalClasses1924 * Additional classes whose methods will be suppressed.1925 * @deprecated Use {@link #suppress(Method[])} instead.1926 */1927 public static synchronized void suppressMethod(Class<?> cls, Class<?>... additionalClasses) {1928 SuppressCode.suppressMethod(cls, additionalClasses);1929 }1930 /**1931 * Suppress all methods for these classes.1932 *1933 * @param classes1934 * Classes whose methods will be suppressed.1935 * @deprecated Use {@link #suppress(Method[])} instead.1936 */1937 public static synchronized void suppressMethod(Class<?>[] classes) {1938 SuppressCode.suppressMethod(classes);1939 }1940 /**1941 * Suppress multiple methods for a class.1942 *1943 * @param clazz1944 * The class whose methods will be suppressed.1945 * @param methodName1946 * The first method to be suppress in class <code>clazz</code>.1947 * @param additionalMethodNames1948 * Additional methods to suppress in class <code>clazz</code>.1949 * @deprecated Use {@link #suppress(Method[])} instead.1950 */1951 public static synchronized void suppressMethod(Class<?> clazz, String methodName, String... additionalMethodNames) {1952 SuppressCode.suppressMethod(clazz, methodName, additionalMethodNames);1953 }1954 /**1955 * Suppress multiple methods for a class.1956 *1957 * @param clazz1958 * The class whose methods will be suppressed.1959 * @param methodNames1960 * Methods to suppress in class <code>clazz</code>.1961 * @deprecated Use {@link #suppress(Method[])} instead.1962 */1963 public static synchronized void suppressMethod(Class<?> clazz, String[] methodNames) {1964 SuppressCode.suppressMethod(clazz, methodNames);1965 }1966 /**1967 * Suppress all methods for this class.1968 *1969 * @param clazz1970 * The class which methods will be suppressed.1971 * @param excludePrivateMethods1972 * optionally not suppress private methods1973 * @deprecated Use {@link #suppress(Method[])} instead.1974 */1975 public static synchronized void suppressMethod(Class<?> clazz, boolean excludePrivateMethods) {1976 SuppressCode.suppressMethod(clazz, excludePrivateMethods);1977 }1978 /**1979 * Suppress a specific method call. Use this for overloaded methods.1980 *1981 * @deprecated Use {@link #suppress(Method)} instead.1982 */1983 public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {1984 SuppressCode.suppressMethod(clazz, methodName, parameterTypes);1985 }1986 @SuppressWarnings("unchecked")1987 private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy,1988 ConstructorArgs constructorArgs, Method... methods) {1989 if (type == null) {1990 throw new IllegalArgumentException("The class to mock cannot be null");1991 }1992 /*1993 * Clear the EasyMock state after the test method is executed.1994 */1995 MockRepository.addAfterMethodRunner(new Runnable() {1996 public void run() {1997 LastControl.reportLastControl(null);1998 }1999 });2000 IMocksControl control = mockStrategy.createMockControl(type);2001 MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());2002 T mock = null;2003 if (type.isInterface()) {2004 mock = control.createMock(type);2005 } else if (type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers())) {2006 Class<?> replicaType = createReplicaType(type, isStatic, constructorArgs);2007 final Object replica = doCreateMock(replicaType, constructorArgs, control, methods);2008 control = mockStrategy.createMockControl(replicaType);2009 MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);2010 final Set<Method> methodsToMock = toSet(methods);2011 if (isStatic) {2012 MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h,2013 methodsToMock, replica));2014 MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);2015 return null;2016 } else {2017 final T newInstance;2018 if (constructorArgs == null) {2019 newInstance = Whitebox.newInstance(type);2020 DefaultFieldValueGenerator.fillWithDefaultValues(newInstance);2021 } else {2022 try {2023 newInstance = (T) constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs());2024 } catch (Exception e) {2025 throw new RuntimeException("Internal error", e);2026 }2027 }2028 MockRepository.putInstanceMethodInvocationControl(newInstance,2029 new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));2030 if (newInstance instanceof InvocationSubstitute<?> == false) {2031 MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance);2032 }2033 return newInstance;2034 }2035 } else {2036 mock = doCreateMock(type, constructorArgs, control, methods);2037 }2038 MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);2039 final Set<Method> methodsToMock = toSet(methods);2040 if (isStatic) {2041 MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h,2042 methodsToMock, mock));2043 MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);2044 } else {2045 MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h,2046 methodsToMock));2047 if (mock instanceof InvocationSubstitute<?> == false) {2048 MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);2049 }2050 }2051 return mock;2052 }2053 private static <T> Class<?> createReplicaType(Class<T> type, boolean isStatic, ConstructorArgs constructorArgs) {2054 ClassReplicaCreator classReplicaCreator = new ClassReplicaCreator();2055 Class<?> replicaType = null;2056 if (isStatic || constructorArgs == null) {2057 replicaType = classReplicaCreator.createClassReplica(type);2058 } else {2059 try {2060 replicaType = classReplicaCreator.createInstanceReplica(constructorArgs.getConstructor().newInstance(2061 constructorArgs.getInitArgs()));2062 } catch (RuntimeException e) {2063 throw e;2064 } catch (InvocationTargetException e) {2065 Throwable targetException = ((InvocationTargetException) e).getTargetException();2066 if (targetException instanceof RuntimeException) {2067 throw (RuntimeException) targetException;2068 }2069 throw new RuntimeException(e);2070 } catch (Exception e) {2071 throw new RuntimeException(e);2072 }2073 }2074 return replicaType;2075 }2076 private static <T> T doCreateMock(Class<T> type, ConstructorArgs constructorArgs, final IMocksControl control,2077 Method... methods) {2078 T mock;2079 MocksControl mocksControl = ((MocksControl) control);2080 if (constructorArgs == null) {2081 if (methods == null) {2082 mock = mocksControl.createMock(type);2083 } else {2084 mock = mocksControl.createMock(type, methods);2085 }2086 } else {2087 if (methods == null) {2088 mock = mocksControl.createMock(type, constructorArgs);2089 } else {2090 mock = mocksControl.createMock(type, constructorArgs, methods);2091 }2092 }2093 return mock;2094 }2095 private static Set<Method> toSet(Method[] methods) {2096 return methods == null ? null : new HashSet<Method>(Arrays.asList(methods));2097 }2098 private static Class<?>[] mergeArgumentTypes(Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {2099 if (firstArgumentType == null) {2100 return additionalArgumentTypes == null ? new Class<?>[0] : additionalArgumentTypes;2101 } else if (additionalArgumentTypes == null) {2102 additionalArgumentTypes = new Class<?>[0];2103 }2104 final Class<?>[] argumentTypes = new Class[additionalArgumentTypes.length + 1];2105 argumentTypes[0] = firstArgumentType;2106 if (additionalArgumentTypes.length != 0) {2107 System.arraycopy(additionalArgumentTypes, 0, argumentTypes, 1, additionalArgumentTypes.length);2108 }2109 return argumentTypes;2110 }2111 @SuppressWarnings("unchecked")2112 private static <T> IExpectationSetters<T> doExpectPrivate(Object instance, Method methodToExpect,2113 Object... arguments) throws Exception {2114 WhiteboxImpl.performMethodInvocation(instance, methodToExpect, arguments);2115 return (IExpectationSetters<T>) org.easymock.EasyMock.expectLastCall();2116 }2117 private static synchronized void replay(Class<?>... types) {2118 for (Class<?> type : types) {2119 final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type);2120 if (invocationHandler != null) {2121 invocationHandler.replay();2122 }2123 NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type);2124 if (newInvocationControl != null) {2125 newInvocationControl.replay();2126 }2127 }2128 }2129 /**2130 * Note: doesn't clear PowerMock state.2131 */2132 private static synchronized void verifyClass(Class<?>... types) {2133 for (Class<?> type : types) {2134 final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type);2135 if (invocationHandler != null) {2136 invocationHandler.verify();2137 }2138 NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type);2139 if (newInvocationControl != null) {2140 try {2141 newInvocationControl.verify();2142 } catch (AssertionError e) {2143 NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type);2144 }2145 }2146 }2147 }2148 private static boolean isNiceReplayAndVerifyMode() {2149 final Boolean mode = (Boolean) MockRepository.getAdditionalState(NICE_REPLAY_AND_VERIFY_KEY);2150 return mode != null && mode;2151 }2152 /**2153 * Clears the state in LastControl that deals with MocksControl.2154 */2155 private static class EasyMockStateCleaner implements Runnable {2156 public void run() {2157 LastControl.reportLastControl(null);2158 clearStateFromOtherClassLoaders();2159 }2160 private void clearStateFromOtherClassLoaders() {2161 for(ClassLoader cl : classloadersToClear())2162 {...

Full Screen

Full Screen

isNiceReplayAndVerifyMode

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.PowerMock;2import org.powermock.core.classloader.annotations.PrepareForTest;3import org.powermock.modules.junit4.PowerMockRunner;4import org.junit.Test;5import org.junit.runner.RunWith;6import static org.junit.Assert.*;7import static org.powermock.api.easymock.PowerMock.*;8@RunWith(PowerMockRunner.class)9@PrepareForTest({PowerMock.class})10public class TestClass {11 public void testMethod() {12 PowerMock.mockStatic(PowerMock.class);13 expect(PowerMock.isNiceReplayAndVerifyMode()).andReturn(true);14 replay(PowerMock.class);15 assertTrue(PowerMock.isNiceReplayAndVerifyMode());16 verify(PowerMock.class);17 }18}19 at org.powermock.api.easymock.internal.mockcreation.MockMethodInterceptor.methodWasNotMocked(MockMethodInterceptor.java:271)20 at org.powermock.api.easymock.internal.mockcreation.MockMethodInterceptor.handleInvocation(MockMethodInterceptor.java:195)21 at org.powermock.api.easymock.internal.mockcreation.MockMethodInterceptor.handleInvocation(MockMethodInterceptor.java:109)22 at org.powermock.api.easymock.internal.mockcreation.MockMethodInterceptor.intercept(MockMethodInterceptor.java:83)23 at org.powermock.api.easymock.PowerMock.isNiceReplayAndVerifyMode(PowerMock.java)24 at com.myproject.MyClass.testMethod(MyClass.java:123)25 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)26 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)27 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)28 at java.lang.reflect.Method.invoke(Method.java:498)29 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)30 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)31 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)32 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

Full Screen

Full Screen

isNiceReplayAndVerifyMode

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.PowerMock;2import org.powermock.modules.testng.PowerMockTestCase;3import org.testng.annotations.Test;4import static org.easymock.EasyMock.*;5public class PowerMockTest extends PowerMockTestCase {6 public void test() {7 PowerMock.resetAll();8 PowerMock.replayAll();9 PowerMock.verifyAll();

Full Screen

Full Screen

isNiceReplayAndVerifyMode

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.PowerMock;2public class Test {3 public static void main(String[] args) {4 boolean niceReplayAndVerifyMode = PowerMock.isNiceReplayAndVerifyMode();5 if (niceReplayAndVerifyMode) {6 PowerMock.setNiceReplayAndVerifyMode(false);7 boolean result = PowerMock.isNiceReplayAndVerifyMode();8 PowerMock.setNiceReplayAndVerifyMode(true);9 return result;10 }11 return PowerMock.isNiceReplayAndVerifyMode();12 }13}14import org.powermock.api.easymock.PowerMock;15public class Test {16 public static void main(String[] args) {17 boolean niceReplayAndVerifyMode = PowerMock.isNiceReplayAndVerifyMode();18 if (niceReplayAndVerifyMode) {19 PowerMock.setNiceReplayAndVerifyMode(false);20 boolean result = PowerMock.isNiceReplayAndVerifyMode();21 PowerMock.setNiceReplayAndVerifyMode(true);22 return result;23 }24 return PowerMock.isNiceReplayAndVerifyMode();25 }26}27import org.powermock.api.easymock.PowerMock;28public class Test {29 public static void main(String[] args) {30 boolean niceReplayAndVerifyMode = PowerMock.isNiceReplayAndVerifyMode();31 if (niceReplayAndVerifyMode) {32 PowerMock.setNiceReplayAndVerifyMode(false);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful