alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

EasyMock example source code file (UsageRangeTest.java)

This example EasyMock source code file (UsageRangeTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - EasyMock tags/keywords

assertionerror, before, illegalargumentexception, iterator, mockcontrol, mockcontrol, range, range, test, test, unexpected, unexpected, usagerangetest, util

The EasyMock UsageRangeTest.java source code

/*
 * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
 * This program is made available under the terms of the MIT License.
 */
package org.easymock.tests;

import static org.junit.Assert.*;

import java.util.Iterator;

import org.easymock.MockControl;
import org.easymock.internal.Range;
import org.junit.Before;
import org.junit.Test;

public class UsageRangeTest {

    private Iterator mock;

    private MockControl<Iterator> control;

    @Before
    public void setup() {
        control = MockControl.createStrictControl(Iterator.class);
        mock = control.getMock();
    }

    @Test
    public void zeroOrMoreNoCalls() {
        mock.hasNext();
        control.setReturnValue(false, MockControl.ZERO_OR_MORE);
        control.replay();
        control.verify();
    }

    @Test
    public void zeroOrMoreOneCall() {
        mock.hasNext();
        control.setReturnValue(false, MockControl.ZERO_OR_MORE);
        control.replay();
        assertFalse(mock.hasNext());
        control.verify();
    }

    @Test
    public void zeroOrMoreThreeCalls() {
        mock.hasNext();
        control.setReturnValue(false, MockControl.ZERO_OR_MORE);
        control.replay();
        assertFalse(mock.hasNext());
        assertFalse(mock.hasNext());
        assertFalse(mock.hasNext());
        control.verify();
    }

    @Test
    public void combination() {
        mock.hasNext();
        control.setReturnValue(true, MockControl.ONE_OR_MORE);
        mock.next();
        control.setReturnValue("1");

        mock.hasNext();
        control.setReturnValue(true, MockControl.ONE_OR_MORE);
        mock.next();
        control.setReturnValue("2");

        mock.hasNext();
        control.setReturnValue(false, MockControl.ONE_OR_MORE);

        control.replay();

        assertTrue(mock.hasNext());
        assertTrue(mock.hasNext());
        assertTrue(mock.hasNext());

        assertEquals("1", mock.next());

        try {
            mock.next();
            fail();
        } catch (AssertionError expected) {
        }

        assertTrue(mock.hasNext());

        assertEquals("2", mock.next());

        assertFalse(mock.hasNext());

        control.verify();

    }

    @Test
    public void withIllegalOwnRange() {
        mock.hasNext();
        try {
            control.setReturnValue(true, new Range(2, 7));
        } catch (IllegalArgumentException e) {
            assertEquals("Unexpected Range", e.getMessage());
        }
    }
}

Other EasyMock examples (source code examples)

Here is a short list of links related to this EasyMock UsageRangeTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.