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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:26:28+00:00 2026-06-05T09:26:28+00:00

I am trying to write a bunch of test codes against a Java class

  • 0

I am trying to write a bunch of test codes against a Java class library and I am using specs2 on Scala.

I have a group of test cases that should be ran for all the subclasses of class T,
but could not figure out the best way to implement this.

Coming from Ruby, I naturally sought something like *shared_examples_for* macro as in Rspec, but there seems to be no such thing in specs2.

Then I thought, subclassing my own Specification subclass with test cases may be one way to go.

Is there any tidier solution to this?

  • 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-05T09:26:29+00:00Added an answer on June 5, 2026 at 9:26 am

    You can find an example of shared examples in the User Guide. Basically this uses the fact that you can define a method taking a specific instance to test and declare examples for it (see the nonEmptyStack method).

    If this helps let me kickstart you with a quick example using a mutable Specification:

     import org.specs2._
    
     class SubclassesSpec extends mutable.Specification {
       "These examples must pass for all subclasses" >> {
         subclasses foreach { subclass => examples(subclass) }
       }
    
       /** @return instances of subclasses of type T where T is 
        * the supertype of the classes you want to test */
       def subclasses: Seq[T] = ...
    
       /** @return examples parameterized by a specific instance */
       def examples(t: T) = {
         "this is the first example" >> {
           // use t here
         }
         "this is the second example" >> {
           // use t here
         }
       }
     }
    

    The only thing you need to check before you do that is the possible side-effects between examples. In the specification above, all the examples are executed concurrently and they share the same object. If that creates any issue, you can either:

    • add sequential at the top of the specification to avoid concurrency

    • change subclasses and examples to def subclasses: Seq[() => T] and def examples(t: () => T) so that you will create a brand new instance for each example

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

Sidebar

Related Questions

I've been trying to write an extend function so I can have bunch of
I'm trying to write a Java function that takes a Class<?> and returns a
I'm trying to write a Lisp macro that writes a bunch of macros, but
Im trying to write something to get my images to show correctly. I have
I am trying to write an ultra simple solution to load a bunch of
I'm trying to write some Elisp code to format a bunch of legacy files.
i am trying to write a validator which could validate the bunch of commas
I'm trying to write some code to automatically configure a bunch of scheduled tasks
I am trying to write simple program using ONLY for loop ( if then
I'm trying to write a Bash script to convert a bunch of files. Assume

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.