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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:07:12+00:00 2026-05-14T02:07:12+00:00

See the following Mock Test by using Spring/Spring-MVC public class OrderTest { // SimpleFormController

  • 0

See the following Mock Test by using Spring/Spring-MVC

public class OrderTest {

    // SimpleFormController
    private OrderController controller;
    private OrderService service;

    private MockHttpServletRequest request;

    @BeforeMethod
    public void setUp() {

        request = new MockHttpServletRequest();
        request.setMethod("POST");

        Integer orderNumber = 421;
        Order order = new Order(orderNumber);

        // Set up a Mock service
        service = createMock(OrderService.class);
        service.save(order);

        replay(service);

        controller = new OrderController();
        controller.setService(service);
        controller.setValidator(new OrderValidator());

        request.addParameter("orderNumber", String.valueOf(orderNumber));
    }

    @Test
    public void successSave() {
        controller.handleRequest(request, new MockHttpServletResponse());

        // Our OrderService has been called by our controller
        verify(service);
    }

    @Test
    public void failureSave() {
        // Ops... our orderNumber is required
        request.removeAllParameters();

        ModelAndView mav = controller.handleRequest(request, new MockHttpServletResponse());

        BindingResult bindException = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");

        assertEquals("Our Validator has thrown one FieldError", bindException.getAllErrors(), 1);
    }

}

As you can see, i do as proposed by Triple A pattern

  • Arrange (setUp method)
  • Act (controller.handleRequest)
  • Assert (verify and assertEquals)

But i would like to test both Mock and Implementation class (OrderService) by using this single Test class. So in order to retrieve my Implementation, i re-write my class as follows

@ContextConfiguration(locations="/app.xml")
public class OrderTest extends AbstractTestNGSpringContextTests {



}

So how should i write my single test to Arrange both Mock and Implementation OrderService without change my Test method (sucessSave and failureSave)

I am using TestNG, but you can show in JUnit if you want

regards,

  • 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-14T02:07:12+00:00Added an answer on May 14, 2026 at 2:07 am

    Again to me it looks like two different tests but if you are going to be adamant 🙂 here it is

    @SpringJunit4Runner(....="app.xml")
    public class OrderTest {
    
        @Resource
        private OrderController controller; //bean from app.xml
    
        @Resource
        private OrderService service; // actual order service
        private OrderService mockOrderService; //some mock order service
        private MockHttpServletRequest request;
    
        @BeforeMethod
        public void setUp() {
            request = new MockHttpServletRequest();
            request.addParameter("orderNumber", String.valueOf(orderNumber));
        }
    
        @Test
        public void successSave() {
            //test with orderService the way you would do it
        }
    
        @Test
        @DirtiesContext
        //need the annotation because we are changing our context beans i.e, ordercontroller
        //so for the next test the context would be recreated
        public void successSaveWithMock() {
            mockOrderService = //create mock service
            orderController.setOrderService(mockOrderService);
            //do the test with mock
        }
    }
    

    This is for JUnit4 but for your TestNG it should be same/similar principle. Again I am assuming you have done your research about why you need an unit test and an integration test in the same file !!!

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

Sidebar

Related Questions

I'm currently building a class using TDD. The class is responsible for waiting for
this error is trown by the following TEST. If i test the DataLayer, the
I want to mock a method of a class and execute a callback which
I'm trying to add unit testing to an ASP.NET MVC application I have built.
For a mock web service I wrote a little Django app, that serves as
Many programming language runtime environments use stacks as their primary storage structure (e.g. see
I am trying to form ribbon XML with the data from the database and
I have an AutoCompleteTextView with a MatrixCursor as an adapter. The AutoCompleteTextView has a
We have restful api over HTTP. Amongst other clients we have also mobile-device clients
I am stuck here. can not think of a clever way to do this.Need

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.