DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > JUnit Testing of Spring MVC application: Testing Frontend Using Selenium

JUnit Testing of Spring MVC application: Testing Frontend Using Selenium

Krishna Prasad user avatar by
Krishna Prasad
·
Mar. 05, 13 · Java Zone · Interview
Like (1)
Save
Tweet
10.90K Views

Join the DZone community and get the full member experience.

Join For Free

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

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.26.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.26.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.26.0</version>
<scope>test</scope>
</dependency>

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,

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<archiveClasses>true</archiveClasses>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

Now let us start exploring the JUnit test for this,

public class SeleniumLoginFrontendTest {

private WebDriver browser;

@Before
public void setup() {
browser = new FirefoxDriver();
}

@Test
public void startTest() {
browser.get("http://localhost:8080/bookstore-example-with-mvc/");

browser.findElement(By.id("login")).click();

// Will throw exception if elements not found
browser.findElement(By.id("username")).sendKeys("jd");
browser.findElement(By.id("password")).sendKeys("secret");

browser.findElement(By.id("loginButton")).click();
browser.findElement(By.id("account")).click();

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

@After
public void tearDown() {
browser.close();
}
}

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.

Reference:

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





 

Spring Framework application JUnit

Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Architecture as Code With C4 and Plantuml
  • 3 Best Tools to Implement Kubernetes Observability
  • 12 Modern CSS Techniques For Older CSS Problems
  • MongoDB vs. DynamoDB Head-to-Head: Which Should You Choose?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo