Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

JUnit Testing of Spring MVC application: Testing Frontend Using Selenium

  • submit to reddit

In continuation of my earlier blogs on Introduction to Spring MVC and Testing Controller in Spring MVC, in this blog I will demonstrate how to test Web layer in Spring MVC. We can also incorporate Authentication and Authorization, learn more about it in the blogs on Spring Security.

The objective of this demo is 2 fold, to build the plumbing for Web layer using TDD and actually test the web layer using Selenium Java API.

For people in hurry, get the latest code from Github and run the below 2 commands in order

mvn clean tomcat7:run

mvn clean test -Dtest=com.example.bookstore.web.frontend.SeleniumLoginFrontendTest

Maven dependency to use Selenium is as below


Also you need to install Selenium IDE plugin for Firefox.

Since this is a web application, we need to build the plumbing for web application, See the com.example.bookstore.web.BookstoreWebApplicationInitializer for more details. Also notice that we don’t need any web.xml if we use the latest Spring 3.0. But when you try to build the project, maven complains that war project should have web.xml. In order to get around this we need to set failOnMissingWebXml to false as shown in the below configuration in pom.xml,


Now let us start exploring the JUnit test for this,

public class SeleniumLoginFrontendTest {

private WebDriver browser;

public void setup() {
browser = new FirefoxDriver();

public void startTest() {


// Will throw exception if elements not found


assertEquals("John", browser.findElement(By.id("firstName")).getAttribute("value"));

public void tearDown() {

Finally run the application in the tomcat7 using Maven command and run the JUnit test as mentioned in the beginning of this blog. The tests will succeed.

I hope this blog helped you. In my next blog, I will talk about how to implement Spring Web flow using TDD.


Pro Spring MVC: With Web Flow by by Marten Deinum, Koen Serneels


Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)