简体   繁体   中英

Good strategy for Spring Framework end-to-end testing

So this is a rather "big" question, but what I'm trying to accomplish is the following:

I have a Spring application, MVC, JDBC (MySQL) and JSP running on tomcat.

My objective is to test the entire "stack" using a proper method.

What I have so far is Junit using Selenium to simulate an actual user interacting with the application (requires a dummy account for that), and performing different validations such as, see if element is present in the page, see if the database has a specific value or if a value matches the database.

1st concern is that this is actually using the database so it's hard to test certain scenarios. I would really like to be able to mock the database. Have it emulate specific account configs, data states etc

2nd concern is that given the fact that I use what is in the database, and data is continuously changing, it is hard to predict behavior, and therefore properly asserting

I looked at Spring Test but it allows for testing outside a servlet container, so no JSP and no Javascript testing possible.

I saw DBUtils documentation but not sure if it will help me in this case

So, to my fellow developers, I would like to ask for tips to:

  • Run selenium tests on top of a mocked database
  • Allow different configs per test
  • Keep compatibility with Maven/Gradle

I have started with an ordered autowire feature to support this kind of stubbing.

It's basically an idea that i took over from the Seam framework i was working with in the past but i couldnt find yet a similar thing in spring.
The idea is to have a precedence annotation (fw, app,mock,...) that will be used to resolve the current implementation of an autowired bean. This is easy already in xml but not with java config.

So we have our normal repository beans in with app precedence and a test package stubbing these classes with mock precedence. If both are in the classpath spring would normally fail with a duplicate bean found exception. In our case the extended beanfactory simply takes the bean with the highest precedence.

Im not sure if the order annotation of spring could be used directly but i prefered to have "well defined" precedence scopes anyway, so it will be clear for our developers what this is about.

! While this is a nice approach to stub so beans for testing i would not use it to replace a database definition but rather go with an inmemory database like hsql, like some previous answers mentionned already. !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM