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

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

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/**
2 * Copyright 2001-2015 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.donlian.test.easymock.samples;
17
18import org.easymock.EasyMock;
19import org.easymock.EasyMockSupport;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23
24/**
25 * Example of how to use <code>org.easymock.classextension.EasyMock</code>
26 * 
27 * @author Henri Tremblay
28 */
29public class BasicClassMockTest extends EasyMockSupport {
30
31    /**
32     * Our nice class that is allowed to print
33     */
34    public static class Document {
35
36        private final Printer printer;
37
38        private String content;
39
40        public Document(Printer printer) {
41            this.printer = printer;
42        }
43
44        public String getContent() {
45            return content;
46        }
47
48        public void setContent(String content) {
49            this.content = content;
50        }
51
52        public void print() {
53            printer.print(content);
54        }
55    }
56
57    /**
58     * The terrible 3rd party class that is not an interface but that we really
59     * want to mock.
60     */
61    public static abstract class Printer {
62        public abstract void print(String toPrint);
63    }
64
65    private Printer printer;
66
67    private Document document;
68
69    @Before
70    public void setUp() {
71        printer = EasyMock.createMock(Printer.class);
72        document = new Document(printer);
73    }
74
75    @After
76    public void tearDown() {
77        printer = null;
78        document = null;
79    }
80
81    @Test
82    public void testPrintContent() {
83        printer.print("Hello world");
84        replayAll();
85
86        document.setContent("Hello world");
87        document.print();
88
89        verifyAll(); // make sure Printer.print was called
90    }
91
92    @Test
93    public void testPrintEmptyContent() {
94        printer.print("");
95        replayAll();
96
97        document.setContent("");
98        document.print();
99
100        verifyAll(); // make sure Printer.print was called
101    }
102}
103
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

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)