|
EasyMock example source code file (UsageTest.java)
The EasyMock UsageTest.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.tests2;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.easymock.tests.IMethods;
import org.junit.Before;
import org.junit.Test;
public class UsageTest {
IMethods mock;
@Before
public void setup() {
mock = createMock(IMethods.class);
}
@Test
public void exactCallCountByLastCall() {
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2");
replay(mock);
assertEquals("Test", mock.oneArg(false));
assertEquals("Test2", mock.oneArg(false));
boolean failed = false;
try {
mock.oneArg(false);
} catch (AssertionError expected) {
failed = true;
}
if (!failed)
fail("expected AssertionError");
}
@Test
public void openCallCountByLastCall() {
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2")
.atLeastOnce();
replay(mock);
assertEquals("Test", mock.oneArg(false));
assertEquals("Test2", mock.oneArg(false));
assertEquals("Test2", mock.oneArg(false));
}
@Test
public void exactCallCountByLastThrowable() {
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2")
.andThrow(new IndexOutOfBoundsException());
replay(mock);
assertEquals("Test", mock.oneArg(false));
assertEquals("Test2", mock.oneArg(false));
try {
mock.oneArg(false);
fail();
} catch (IndexOutOfBoundsException expected) {
}
boolean failed = true;
try {
mock.oneArg(false);
failed = false;
} catch (AssertionError expected) {
}
if (!failed)
fail("expected AssertionError");
}
@Test
public void openCallCountByLastThrowable() {
expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2")
.andThrow(new IndexOutOfBoundsException()).atLeastOnce();
replay(mock);
assertEquals("Test", mock.oneArg(false));
assertEquals("Test2", mock.oneArg(false));
try {
mock.oneArg(false);
} catch (IndexOutOfBoundsException expected) {
}
try {
mock.oneArg(false);
} catch (IndexOutOfBoundsException expected) {
}
}
@Test
public void moreThanOneArgument() {
expect(mock.threeArgumentMethod(1, "2", "3")).andReturn("Test")
.times(2);
replay(mock);
assertEquals("Test", mock.threeArgumentMethod(1, "2", "3"));
boolean failed = true;
try {
verify(mock);
failed = false;
} catch (AssertionError expected) {
assertEquals(
"\n Expectation failure on verify:"
+ "\n threeArgumentMethod(1, \"2\", \"3\"): expected: 2, actual: 1",
expected.getMessage());
}
if (!failed) {
fail("exception expected");
}
}
@Test
public void wrongArguments() {
mock.simpleMethodWithArgument("3");
replay(mock);
try {
mock.simpleMethodWithArgument("5");
fail();
} catch (AssertionError expected) {
assertEquals(
"\n Unexpected method call simpleMethodWithArgument(\"5\"):"
+ "\n simpleMethodWithArgument(\"3\"): expected: 1, actual: 0",
expected.getMessage());
}
}
@Test
public void summarizeSameObjectArguments() {
mock.simpleMethodWithArgument("3");
mock.simpleMethodWithArgument("3");
replay(mock);
try {
mock.simpleMethodWithArgument("5");
fail();
} catch (AssertionError expected) {
assertEquals(
"\n Unexpected method call simpleMethodWithArgument(\"5\"):"
+ "\n simpleMethodWithArgument(\"3\"): expected: 2, actual: 0",
expected.getMessage());
}
}
@Test
public void argumentsOrdered() {
mock.simpleMethodWithArgument("4");
mock.simpleMethodWithArgument("3");
mock.simpleMethodWithArgument("2");
mock.simpleMethodWithArgument("0");
mock.simpleMethodWithArgument("1");
replay(mock);
try {
mock.simpleMethodWithArgument("5");
fail("exception expected");
} catch (AssertionError expected) {
assertEquals(
"\n Unexpected method call simpleMethodWithArgument(\"5\"):"
+ "\n simpleMethodWithArgument(\"4\"): expected: 1, actual: 0"
+ "\n simpleMethodWithArgument(\"3\"): expected: 1, actual: 0"
+ "\n simpleMethodWithArgument(\"2\"): expected: 1, actual: 0"
+ "\n simpleMethodWithArgument(\"0\"): expected: 1, actual: 0"
+ "\n simpleMethodWithArgument(\"1\"): expected: 1, actual: 0",
expected.getMessage());
}
}
@Test
public void mixingOrderedAndUnordered() {
mock.simpleMethodWithArgument("2");
mock.simpleMethodWithArgument("1");
checkOrder(mock, true);
mock.simpleMethodWithArgument("3");
mock.simpleMethodWithArgument("4");
checkOrder(mock, false);
mock.simpleMethodWithArgument("6");
mock.simpleMethodWithArgument("7");
mock.simpleMethodWithArgument("5");
replay(mock);
mock.simpleMethodWithArgument("1");
mock.simpleMethodWithArgument("2");
boolean failed = false;
try {
mock.simpleMethodWithArgument("4");
} catch (AssertionError e) {
failed = true;
}
if (!failed) {
fail();
}
mock.simpleMethodWithArgument("3");
mock.simpleMethodWithArgument("4");
mock.simpleMethodWithArgument("5");
mock.simpleMethodWithArgument("6");
mock.simpleMethodWithArgument("7");
verify(mock);
}
}
Other EasyMock examples (source code examples)Here is a short list of links related to this EasyMock UsageTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.