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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:36:13+00:00 2026-06-07T05:36:13+00:00

Let’s imagine a Spring 3.1 web mvc application using MySql via Hibernate JPA via

  • 0

Let’s imagine a Spring 3.1 web mvc application using MySql via Hibernate JPA via DAO’s.

One has to write tests for the controllers without deploying the application and using an in-memory database (like hsqldb:mem for example). In other words, one should be able to run tests on a local PC.

How can this be achieved? Can someone provide a detailed example of how to write tests for such a controller? How should @ContextConfiguration be configured? How to retrieve a WebApplicationContext for testing purposes locally? Should one maintain a hibernate-test.cfg.xml?

Update

I am not talking only about unit testing, I am talking about integration testing.

Solution

To make this complete on top of Sean Patrick Floyd’s answer, here is a solution inspired from here:

Example of service:

public interface MarkingService {
    public String markItem(String item);
}

Example of controller using service

@Controller  
public class TestableController {

    @Autowired
    private MarkingService markerService;

    @RequestMapping(value = "mark/{name}")  
    public String mark(Model ui, @PathVariable String name){  

        String value = this.markerService.markItem(name);
        ui.addAttribute("mark-value", value);

        return "mark-show";  

    }  

}

Mock of service to test controller:

public class MarkingServiceMock implements MarkingService {

    @Override
    public String markItem(String item) {
        return "mockValue";
    }

}

Config object for test configuration:

@Configuration
public class TestableControllerConfig {

    // We return a mock to test the controller layer
    @Bean
    public MarkingService markingService() {
        return new MarkingServiceMock();
    }

}

Controller test:

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(classes={TestableControllerConfig.class})
public class TestableControllerTest {

    // Will be injected with TestableControllerConfig.markingService()
    @Autowired
    private MarkingService markerService;

    @Test
    public void testController() {

        // ...

    }

}

Assuming the service layer relies on DAO’s, it is easy to inject implementations based on in-memory db too.

  • 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-07T05:36:15+00:00Added an answer on June 7, 2026 at 5:36 am

    Usually, you wouldn’t test the whole setup. Write tests for the web layer and mock the persistence layer in them. Write separate tests for the service and persistence layer.

    If you are going to test web controllers, use ModelAndViewAssert or the Mock versions of HttpServletRequest etc.

    If on the other hand you want to test the whole setup, run automated web tests using HtmlUnit, Selenium etc., but do it on your real deployed app (of course on a non-public installation).

    But in my opinion, if you want to test the big picture, you need to test the real thing. Mocking on a large scale gets very messy.

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

Sidebar

Related Questions

Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I'm writing a Windows Forms (.NET Framework 3.5) application which shows the
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's aggregate a list of free quality web site design templates. There are a

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.