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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:53:05+00:00 2026-05-27T21:53:05+00:00

I am writing unit tests for service layer in my spring application. Here is

  • 0

I am writing unit tests for service layer in my spring application.
Here is my service class

    @Service
    public class StubRequestService implements RequestService {    
        @Autowired
        private RequestDao requestDao;  

        @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
        @Override
        public Request getRequest(Long RequestId) {
            Request dataRequest = requestDao.find(requestId);
            return dataRequest;
        }
    }  

Here is my test class

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
public class StubRequestServiceTest {

    @Mock
    public RequestDao requestDao;

    StubRequestService stubRequestService;  // How can we Autowire this ?

    @org.junit.Before
    public void init() {
      stubRequestService = new StubRequestService();  // to avoid this 
      stubRequestService.setRequestDao(dataRequestDao);  
      // Is it necessary to explicitly set all autowired elements ?  
      // If I comment/remove above setter then I get nullPointerException 
    }

    @Test
    public void testGetRequest()  {
        Request request = new Request();
        request.setPatientCnt("3");
        when(requestDao.find(anyLong())).thenReturn(request);
        assertEquals(stubRequestService.getRequest(1234L).getPatientCnt(),3);
    }    
}   

Its working fine but I have few questions

  1. How can we Autowire service class in test ? I am using constructor in init() method to create service object.
  2. Do we have to set all Autowire element for service class ? For ex StubRequestService have autowired RequestDao which I need to set explicitly before calling test method otherwise it giveds nullPointerException as requestDao is null in StubRequestService.getRequest method.
  3. Which are the good practices to follow while unit testing Spring service layer ? (If I am doing anything wrong).
  • 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-27T21:53:06+00:00Added an answer on May 27, 2026 at 9:53 pm
    1. If you really feel that it will make your tests easier to understand – you can initialize a spring context and fetch all of the objects from there. However, usually it will require creating a separate spring configuration XML file specifically for tests therefore I would not recommend it.

      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("testApplicationContext.xml");
      stubRequestService = (RequestService)applicationContext.getBean("myRequestServiceBean");
      
    2. (and 3) Basically, I prefer testing each component of my application in total isolation from eachother and that’s why I do not recommend what I described in [1].

    What that means, is you take a separate logical slice of your application and test only it, while fully mocking up everything it tries to access.

    Let’s say you have three classes:

    //Fetches stuff from some webservice and converts to your app domain POJOs
    class DataAccessLayer {
        public void setWebservice(Webservice ws) {...};
    
        public MyObject getMyObject() {...};
    }
    
    //Formats the domain POJOs and sends them to some kind of outputstream or stuff.
    class ViewLayer {
        public void setOutputStream(OutputStream os) {...};
    
        public void viewMyObject(MyObject mo) {...};
    }
    
    //Main entry point of our MyObject fetch-process-display workflow
    class Controller {
        public void setDataAccessLayer(DataAccessLayer dal) {...};
        public void setViewLayer(ViewLayer vl) {...};
    
        public void showMyObject() {
            MyObject mo = dal.getMyObject();
            ...some processing here maybe...
            vl.viewMyObject(mo);
        }
    }
    

    Now, what tests can we write here?

    1. Test if DataAccessLayer properly converts the object from mocked up WS to our domain object.
    2. Test if ViewLayer properly formats the object given to him and writes it to mocked up output stream.
    3. Test if Controller takes an object from mocked up DataAccessLayer processes it properly and sends it to mocked up ViewLayer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing unit tests with C#, NUnit and Rhino Mocks. Here are the
I had to start writing some unit tests, using QualityTools.UnitTestFramework, for a web service
I'm in the process of writing unit tests for a JEE5 web service. The
I'm currently writing unit tests for an android application and stumbled into the following
I'm currently writing a special client application to allow our unit tests to work
I'm having some trouble with my unit tests in a Spring MVC application. In
I decided to start writing unit tests in our application. It uses Entity Framework
When writing unit tests, do you place your tests inside the assembly you wish
I am writing unit tests for some of my code and have run into
I have been writing unit tests using NUnit and Moq with my Silverlight code

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.