Email Subscription Form

Tuesday, June 11, 2013

Setting up Maven

In order to use Webdriver, you will need to have Maven installed and set up.  Fortunately, the helpful people at Apache Software have created an excellent tutorial about how to set up Maven:

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

It probably won't take you five minutes as advertised, but it will take you less than a day.  Don't worry too much about what you are doing as you are running these commands; once Maven is set up, we only have to think about the commands we need to run our automated tests.

Now let's return to Eclipse.  The last time we were in Eclipse we created a project called MyFirstJavaProject.  Right-click on this project and select "Configure-> Convert to Maven project".  A popup window will appear.   Click "Finish" on the window, and wait for the configuration to complete.  When it's done, you'll notice that your project icon now has a little M next to the J.  You'll also notice that you have a pom.xml file in your project folder.  We'll learn more about the POM in a future blog post.

Let's create a JUnit test.  First we'll run it from Eclipse, then we'll run it from the command line using Maven.

1. Right-click on the MyFirstJavaProject and select New->Source Folder
2. Give the new folder a name of src/test/java (click the "Update exclusion filters" checkbox if needed)
3 Right-click on the new folder and select New->Package
4. Give the package a name of myFirstTests
5. Now right-click on the myFirstTests package and select New->Class
6. Give the class a name of FirstTest
7. The FirstTest class should open in the center pane, and should look like this:
package myFirstTests;
public class FirstTest {
}
8. In between the brackets, add the following:
@Test
public void test() {
int x = 2;
int y = 3;
int total = x+y;
assertEquals(total, 5);
}
9. Right-click on the Project name (MyFirstJavaProject) and choose Build Path->Add Libraries
10. Select JUnit
11. Choose JUnit 4 and click Finish
12. Look again at the lines of code you added to the FirstTest class.  There will be two lightbulb icons on the left side of the code.
13. Right-click on the one next to "@Test" and choose Add JUnit4 to Build Path
14. Right-click on the one next to "assertEquals(total,5)" and choose Import org.junit.Assert;
15. Now near the top of the page, you will see
import static org.junit.Assert.*;
import org.junit.Test;
16. Save your changes by clicking on the disk icon in the toolbar
17. Right-click on the FirstTest.java file in the left pane, and choose Run As-> JUnit Test
18. Your test should run, and a new tab named JUnit should appear in the left pane
19. Click on the left tab and notice that the window displays what test has been run, and that there is a green bar across the pane indicating that the test has passed

Now let's try running the test from the command line. First we'll need to make a change to the pom.xml file:

1. Double-click on the pom.xml file to open it in the center pane
2. Click the tab at the bottom of the pane that says pom.xml
3. You should now be viewing the file in xml format
4. Add this text to the xml file, right underneath </build>:
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
  </dependencies>
5. Save your changes
6. Now open the command window
7. Change directories until you are in your project folder (MyFirstJavaProject)
8. Run this command:
mvn clean test
9. Your project should build, your test should run from the command line, and you should be notified that the build is successful

Congratulations!  You have run your first test from Maven!





New Blog Location!

I've moved!  I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted.  So I&#...