How to use method of org.easymock.samples.ConstructorCalledMockTest class

Best Easymock code snippet using org.easymock.samples.ConstructorCalledMockTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2003-2007 OFFIS, Henri Tremblay. 
3 * This program is made available under the terms of the MIT License.
4 */
5package org.easymock.classextension.samples;
6
7import static org.easymock.EasyMock.*;
8import static org.easymock.classextension.EasyMock.*;
9import static org.junit.Assert.*;
10
11import java.math.BigDecimal;
12
13import org.easymock.classextension.ConstructorArgs;
14import org.junit.After;
15import org.junit.Before;
16import org.junit.Test;
17
18public class ConstructorCalledMockTest {
19
20    /**
21     * Class to test and partially mock
22     */
23    public static abstract class TaxCalculator {
24
25        private final BigDecimal[] values;
26
27        public TaxCalculator(BigDecimal... values) {
28            this.values = values;
29        }
30
31        protected abstract BigDecimal rate();
32
33        public BigDecimal tax() {
34            BigDecimal result = BigDecimal.ZERO;
35
36            for (BigDecimal d : values) {
37                result = result.add(d);
38            }
39
40            return result.multiply(rate());
41        }
42    }
43
44    private TaxCalculator tc;
45
46    @Before
47    public void setUp() {
48        ConstructorArgs args = new ConstructorArgs(TaxCalculator.class
49                .getConstructors()[0], // get the one and only constructor
50                (Object) new BigDecimal[] { new BigDecimal("5"), // (Object) cast required to tell the compiler the array is the first param of the varargs
51                        new BigDecimal("15") });
52
53        tc = createMock(TaxCalculator.class, args); // no need to mock any methods, abstract ones are mocked by default
54    }
55
56    @After
57    public void tearDown() {
58        verify(tc);
59    }
60
61    @Test
62    public void testTax() {
63
64        expect(tc.rate()).andStubReturn(new BigDecimal("0.20"));
65        replay(tc);
66
67        assertEquals(new BigDecimal("4.00"), tc.tax());
68    }
69
70    @Test
71    public void testTax_ZeroRate() {
72
73        expect(tc.rate()).andStubReturn(BigDecimal.ZERO);
74        replay(tc);
75
76        assertEquals(BigDecimal.ZERO, tc.tax());
77    }
78}
79
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 used method in ConstructorCalledMockTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)