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 8888837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:07:08+00:00 2026-06-14T22:07:08+00:00

I have a defined test method chain in test class using Spec2: def is

  • 0

I have a defined test method chain in test class using Spec2:

def is =
  "EntriesServlet with logged user" ^
  "POST / request should update entry that user owns" ! updateExistingEntryThatLoggedUserOwns ^
  "POST / request should not update non existing entry" ! notUpdateNonExistingEntry ^
  "POST / request should not update non owner entry" ! notAllowToUpdateNotOwnedEntry
end

and in these methods I am checking if defined mock were called. But I need to recreate one mock so I could count invocations only for one method not globally.

So what I need is a way to seamlessly define method let’s say:

 def prepareMocks = {
   serviceMock = mock[MyService]
 }

that will be executed before each test method so I have clean mock ready before checking my assertions.

I tried with traits BeforeEach and BeforeExample from Spec2 but they are not what I am looking for.

  • 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-14T22:07:09+00:00Added an answer on June 14, 2026 at 10:07 pm

    You can use case classes to instantiate your mocks and isolate them from other examples being executed at the same time:

    import org.specs2._
    import specification._
    import mock._
    
    class MySpec extends Specification { def is =
      "EntriesServlet with logged user" ^
        "POST / request should update entry that user owns" !        c().updateExistingEntryThatLoggedUserOwns ^
        "POST / request should not update non existing entry" ! c().notUpdateNonExistingEntry ^
        "POST / request should not update non owner entry" ! c().notAllowToUpdateNotOwnedEntry ^
      end
    
      trait MyService
      case class c() extends Mockito {
        val service = mock[MyService]
    
        def updateExistingEntryThatLoggedUserOwns = service must not beNull
        def notUpdateNonExistingEntry = ok
        def notAllowToUpdateNotOwnedEntry = ok
      }
    }
    
    // here's a similar solution using standardised group names which is a 1.12.3 feature
    
    class MySpec extends Specification { def is =
      "EntriesServlet with logged user" ^
        "POST / request should update entry that user owns"   ! g1().e1 ^
        "POST / request should not update non existing entry" ! g1().e2 ^
        "POST / request should not update non owner entry"    ! g1().e3 ^
      end
    
      trait MyService
      "POST requests" - new g1 with Mockito {
        val service = mock[MyService]
    
        e1 := { service must not beNull }
        e2 := ok
        e3 := ok
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a class code.py class test: def __init__(self.x,self.y): self.x = x self.y
On Windows using WAMPserver (Apache, MySQL, PHP) I have the following: //test.php if (!defined('LC_MESSAGES'))
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have defined the following method: def some_method x = x + 1 y
I have a class defined like so: public class Test { static <T> List<Class<T>>
I have defined the following two functions test <- function(t) { return( (0.5*eta^2/theta)*(1-exp(-2*theta*t)) )
I have a table defined by the following SQL: CREATE TABLE test ( id
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT
I have a class that defines its own enum like this: public class Test
If I have a method chain like the following: var abc = new ABC();

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.