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

  • SEARCH
  • Home
  • 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 3999344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:38:44+00:00 2026-05-20T07:38:44+00:00

Referred this question first. But seems my context is different. I’ll try to be

  • 0

Referred this question first. But seems my context is different.

I’ll try to be short and simple. (Just the code I’m putting out is quite big 😉

I have some 50+ service classes. And need to write the unit test cases for all of them.
Across all these test classes, some tests are common. (delete, find etc) Just the object type would differ across the service classes.

Following example would clear the picture.


Consider following service class which has CRUD operations.

public class ObjService {

public Obj addObj(ParamType param, String var)  { ... }

public void deleteObj(ParamType param, String var)  { ... }

public List<Obj> findAllObj(ParamType param, String var) { ... }

public Obj findById(ParamType param, String var, String objIdToFind) { .. }

public List<Obj> getAllObjs(ParamType param, String var, ObjQuery objQuery) throws Exception { ... }

public Obj updateObj(ParamType param,
        String var, Obj objToUpdate) throws Exception { }
}

Now I’m writing a test case for ObjService class. (Test Framework – testNG)

public class ObjServiceTest {

//These methods which will differ across all service classes

@Test
public void testAddObj() throws Exception {
    addObj();
}

@Test
public void testUpdateObj() throws Exception {
    Obj objToUpdate = addObj();
    Obj updatedObj = updateObj(objToUpdate);
}

public Obj addObj() throws Exception {
    //add obj test data and return the obj object       
}

public Obj updateObj(Obj objToUpdate) throws Exception {
    //update obj test data and return the updated obj object    
}

//Following methods will be common to all classes. Except the name 'obj'
//e.g. For obj2 it would change to testDeleteObj2() { Obj2 obj2Todelete.... etc}

@Test
public void testDeleteObj() throws Exception {
    Obj objToDelete = addObj();
    deleteObj(objToDelete);
}

public void deleteObj(Obj objToDelete) throws Exception {
    //delete the obj object
}

@Test
public void testFindById() throws Exception {
    ObjService client = new ObjService();
    List<Obj> objs = dsClient.findAllObj(...);
} 

@Test
public void testFindAllObjs() throws Exception {}

@Test
public void testGetObjs() throws Exception {}
}

Now. Writing the common methods manually for all classes is surely a time consuming job. So can it be reduced by doing some automation?

(Tried my best to put the question in least baffling way)

Edit: 1) The test classes already inherit a BaseTestClass which contains the initial setup needed. So that is a problem.

2) Please don’t forget the part, where
refactoring is needed across the
methods which differ.

  • 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-05-20T07:38:44+00:00Added an answer on May 20, 2026 at 7:38 am

    It sounds like your services should implement some generic interface. That way you could write an abstract base test case which is also generic, then make each “real” service test inherit from it, including inheriting the tests within that abstract class.

    The constructor for the subclass would pass in the appropriate values for things like the service, a sample query etc.

    EDIT: For the base class, just make the abstract base test class subclass your existing base class.

    For specialization, either override the test method itself when it needs to do a completely different thing, or make the test methods depend on abstract methods in the abstract class, so that each concrete subclass can fill in the appropriate behaviour.

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

Sidebar

Related Questions

I've already referred to this SO post . I've been embedding images using an
A while ago another question referred to the (possibly urban tale) statistic that ...
I recently asked a question about what I called method calls. The answer referred
I am a Scala novice so forgive me if this is a stupid question,
I ran into my first compiler that changes the lvalue passed to ::delete, but
I just got referred to stackoverflow by a friend here to help with a
I am going to install first application in my android phone, but having some
I am learning Servlets concept. Initially, I Tutorial referred to this link and working
C++ 0x has template aliases (sometimes referred to as template typedefs). See here .
Commonly, a website has a menu bar with links to different pages of the

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.