1

Ok, I come from Rails am having a bit of a hard time trying to get this working.

Right know this is what I understand from the Spring framework (and please correct me if I'm wrong):

  1. I am using Tomcat. So what it does, basically, is go after web.xml, and check any configuration files from there in order to initialize and get beans auto wired, etc.

  2. In my example, I have on web.xml the context config location as application-config.xml

  3. It turns out that application-config.xml has other config files that will take care of the following:

Hibernate:

<util:properties id="hibernatePropertiesConfigurer" location="classpath:hibernate.properties"/>

JSon Converter:

<import resource="classpath:json-converter.xml"/>

Managers (@Component) Scanner:

<import resource="classpath:scanner-config.xml"/>

Among others. In other words, these work just fine, web server gets up, managers (@Components) are @Autowired, @Controllers can call them, persist objects in the database, etc.

Now, I want to test, i.e. run JUnit tests. I did find a lot of examples on how to mock these layers (managers, controllers) but I want to try the real thing:

  • Test will instantiate @Controller
  • Test will post to @Controller object
  • @Controller will have a real @Autowired (not mocked version) of @Component or @Bean
  • @Component will persist an @Entity
  • And, most important of all, database should see the changes. (I am using MySQL)
  • Test will, then, use an @Autowired instance of @Component, and query the database to confirm the persistence occurred.

Is this possible, at all, with Spring, JUnit testing?

marcelorocks
  • 2,005
  • 3
  • 18
  • 28
  • 1
    With Spring 3.2, check out `MockMvc` and the whole MVC Test stack. – Sotirios Delimanolis Mar 13 '14 at 19:10
  • Unfortunately I am stuck with Spring 3.1. However, I looked at this: https://github.com/spring-projects/spring-mvc-showcase.git and even though it was very useful, I want to try things without mocking. In other words, I want to have the same application context I have for web, on tests – marcelorocks Mar 13 '14 at 19:14
  • There was a preview of `MockMvc` that worked with 3.1: https://github.com/spring-projects/spring-test-mvc. Maybe it still does (but you really need to upgrade)? – Dave Syer Mar 13 '14 at 19:25
  • @DaveSyer thanks, but I am trying quite the opposite: stay away from mocking for now. I want to test with real objects and real transactions to the database. – marcelorocks Mar 13 '14 at 19:30
  • 1
    You probably haven't tried it then. `MockMvc` only mocks the HTTP protocol (actually the Servlet container). Everything else is as real as you want it to be. – Dave Syer Mar 13 '14 at 20:02
  • I actually did look at it, but maybe I am missing something, I could not find any Entity, Controller or Bean there. – marcelorocks Mar 13 '14 at 20:15
  • 1
    http://stackoverflow.com/questions/12607140/how-to-test-post-spring-mvc Are u talking about this? – CodeBender Mar 14 '14 at 06:23
  • @CodeBender that appears to be an example of using MockMVc with `@Transactional` which is what @marcelrooks appears to need. There is copious documentation on the interweb (e.g. spring.io website). – Dave Syer Mar 14 '14 at 11:48
  • @CodeBender that is close, yes. It is only the controller part but still good. And Dave, you are also right about transaction. But quite honestly spring.io website is not test friendly at all. When I go to docs or guides and search for 'test' it gets me nothing. So maybe there is something deep there but it is a very tedious process I have already tried to go for. – marcelorocks Mar 14 '14 at 15:05
  • Totally understand where you're coming from especially coming from a rails background. Haha – CodeBender Mar 14 '14 at 15:24

1 Answers1

0

If you want to verify the behavior of the entire stack like that, it's no longer a unit test, but an integration test.

If you're using Maven, you can use something like the Maven Jetty plugin to deploy your project into an embedded container during the pre-integration-test phase. Then in your tests, make the HTTP calls to the server at localhost, and verify you get the expected responses. Then, in the post-integration-test phase, shut down the Jetty server.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
  • Does it mean that even if I didn't want to test the controller, but rather the persistence layer alone, I would still need to get something like this up in order to be able to store for real on my database? And what about the database pre-defined data (fixtures) ? Does this take care of that too? – marcelorocks Mar 13 '14 at 19:22
  • Not sure what you mean by database fixtures. All this does is deploy your webapp 'from scratch' into a throwaway container, so that you can run HTTP requests against it as though you were a client. If you're looking to test individual classes, then without the MockMVC framework you're probably out of luck. – Thorn G Mar 13 '14 at 19:24