@@ -0,0 +1,2591 @@
+/*%0D%0A * Copyright 2009 the original author or authors.%0D%0A *%0D%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0D%0A * you may not use this file except in compliance with the License.%0D%0A * You may obtain a copy of the License at%0D%0A *%0D%0A *      http://www.apache.org/licenses/LICENSE-2.0%0D%0A *%0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0D%0A * See the License for the specific language governing permissions and%0D%0A * limitations under the License.%0D%0A */%0D%0A%0D%0Apackage samples.junit4.mockpolicy;%0D%0A%0D%0Aimport static org.easymock.EasyMock.expect;%0D%0Aimport static org.junit.Assert.assertEquals;%0D%0Aimport static org.powermock.api.easymock.PowerMock.createMock;%0D%0Aimport static org.powermock.api.easymock.PowerMock.replay;%0D%0Aimport static org.powermock.api.easymock.PowerMock.verifyAll;%0D%0A%0D%0Aimport java.lang.reflect.Method;%0D%0A%0D%0Aimport org.junit.Test;%0D%0Aimport org.junit.runner.RunWith;%0D%0Aimport org.powermock.core.classloader.annotations.MockPolicy;%0D%0Aimport org.powermock.core.spi.PowerMockPolicy;%0D%0Aimport org.powermock.mockpolicies.MockPolicyClassLoadingSettings;%0D%0Aimport org.powermock.mockpolicies.MockPolicyInterceptionSettings;%0D%0Aimport org.powermock.modules.junit4.PowerMockRunner;%0D%0Aimport org.powermock.reflect.Whitebox;%0D%0A%0D%0Aimport samples.mockpolicy.ResultCalculator;%0D%0Aimport samples.mockpolicy.SimpleClassWithADependency;%0D%0A%0D%0A@RunWith(PowerMockRunner.class)%0D%0A@MockPolicy(MockPolicyExpectationsExample.class)%0D%0Apublic class MockPolicyWithExpectationsTest %7B%0D%0A%0D%0A%09@Test%0D%0A%09public void mockPolicyWithExpectationsWorks() throws Exception %7B%0D%0A%09%09final SimpleClassWithADependency tested = new SimpleClassWithADependency();%0D%0A%09%09Whitebox.setInternalState(tested, new ResultCalculator(5));%0D%0A%09%09%0D%0A%09%09assertEquals(2.0, tested.getResult(), 0.0);%0D%0A%09%09%0D%0A%09%09verifyAll();%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aclass MockPolicyExpectationsExample implements PowerMockPolicy %7B%0D%0A%09public void applyClassLoadingPolicy(MockPolicyClassLoadingSettings settings) %7B%0D%0A%09%09settings.addFullyQualifiedNamesOfClassesToLoadByMockClassloader(ResultCalculator.class.getName());%0D%0A%09%7D%0D%0A%0D%0A%09public void applyInterceptionPolicy(MockPolicyInterceptionSettings settings) %7B%0D%0A%09%09final ResultCalculator calculatorMock = createMock(ResultCalculator.class);%0D%0A%09%09expect(calculatorMock.calculate()).andReturn(2.0);%0D%0A%0D%0A%09%09replay(calculatorMock);%0D%0A%0D%0A%09%09Method calculateMethod = Whitebox.getMethod(ResultCalculator.class, %22calculate%22);%0D%0A%09%09settings.addSubtituteReturnValue(calculateMethod, calculatorMock.calculate());%0D%0A%09%7D%0D%0A%7D%0D%0A

Test that demonstrates simple non-dynamic expectations in a mock policy