Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:32:03+00:00 2026-06-06T19:32:03+00:00

It seems like EasyMock tests tend to follow the following pattern: @Test public void

  • 0

It seems like EasyMock tests tend to follow the following pattern:

@Test
public void testCreateHamburger()
{
  // set up the expectation
  EasyMock.expect(mockFoodFactory.createHamburger("Beef", "Swiss", "Tomato", "Green Peppers", "Ketchup"))
    .andReturn(mockHamburger);

  // replay the mock
  EasyMock.replay(mockFoodFactory);

  // perform the test
  mockAverager.average(chef.cookFood("Hamburger"));

  // verify the result
  EasyMock.verify(mockFoodFactory);
}

This works fine for one test, but what happens when I want to test the same logic again in a different method? My first thought is to do something like this:

@Before
public void setUp()
{
  // set up the expectation
  EasyMock.expect(mockFoodFactory.createHamburger("Beef", "Swiss", "Tomato", "Green Peppers", "Ketchup"))
    .andReturn(mockHamburger);

  // replay the mock
  EasyMock.replay(mockCalculator);
}

@After
public void tearDown()
{
  // verify the result
  EasyMock.verify(mockCalculator);
}

@Test
public void testCreateHamburger()
{
  // perform the test
  mockAverager.average(chef.cookFood("Hamburger"));
}

@Test
public void testCreateMeal()
{
  // perform the test
  mockAverager.average(chef.cookMeal("Hamburger"));
}

There’s a few fundamental problems with this approach. The first is that I can’t have any variation in my method calls. If I want to test person.cookFood("Turkey Burger"), my set up method wouldn’t work. The second problem is that my set up method requires createHamburger to be called. If I call person.cookFood("Salad"), then this might not be applicable. I could use anyTimes() or stubReturn() with EasyMock to avoid this problem. However, these methods only verify if a method is called, it’s called with certain parameters, not if the method was actually called.

The only solution that’s worked so far is to copy and paste the expectations for every test and vary the parameters. Does anybody know any better ways to test with EasyMock which maintain the DRY principle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:32:04+00:00Added an answer on June 6, 2026 at 7:32 pm

    The problems you are running into are because Unit Tests should be DAMP not DRY. Unit tests will tend to repeat themselves. If you can remove the repetition in a safe way (so that it doesnt create unnecessarily coupled tests), then go for it. If not, then don’t force it. Unit tests should be quick and easy…if they aren’t then you are spending too much time testing instead of writing business value.

    Just my two cents. BTW, the Art of Unit Testing by Roy Osherove is a great read on unit testing, and covers this topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like the slow Tomcat 7 startup problem can be resolved with metadata-complete set
Seems like tooltips that you get from attribute title=test are not displayed inside the
Seems like every C# static analyzer wants to complain when it sees a public
Seems like Moq is caching data I set up as return. When I do
Seems like there are two products with similar names, but isn't what I expect.
Seems like I may be missing to set some enviornment variable in my AppHarbor
Seems like i need some help with a project. I have a routine ,
Seems like there should be... Right now it just seems like magic that you
Seems like cuke doesn't show the full error message (at least when problem occurs
Seems like a standard approach for an ioc when given a scenario like (C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.