Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Training Video: Test First Development using Eclipse"

 
Line 13: Line 13:
 
"My first step is to create a Java project. I’ll use the new project wizard to do that."
 
"My first step is to create a Java project. I’ll use the new project wizard to do that."
  
[[Image:Tryit.gif]] Create a new project named "Banking"
+
*Create a new project named "Banking"
  
 
"With the project created, my next step is to create a test class."
 
"With the project created, my next step is to create a test class."
  
[[Image:Tryit.gif]] Create a new JUnit test class named "BankingTests".
+
*Create a new JUnit test class named "BankingTests".
  
 
"When I attempt to do this, Eclipse recognizes that I’m going to need access to the junit.jar library and offers to add it to my build path."
 
"When I attempt to do this, Eclipse recognizes that I’m going to need access to the junit.jar library and offers to add it to my build path."
  
[[Image:Tryit.gif]] Provide a package "org.eclipse.banking.tests" and test class "BankingTests".
+
*Provide a package "org.eclipse.banking.tests" and test class "BankingTests".
  
 
"Eclipse automatically opens the source file for this new test class."
 
"Eclipse automatically opens the source file for this new test class."
Line 27: Line 27:
 
"Now, I'll add my first test method."
 
"Now, I'll add my first test method."
  
[[Image:Tryit.gif]] Type "test".
+
*Type "test".
  
 
"Eclipse is very helpful for lazy people like me. Having typed 'test', I now hold down the control key and hit the spacebar."
 
"Eclipse is very helpful for lazy people like me. Having typed 'test', I now hold down the control key and hit the spacebar."
  
[[Image:Tryit.gif]] Type ctrl-space
+
*Type ctrl-space
  
 
"Eclipse recognizes that I'm trying to create a test method and, if I select this first entry in the list of suggestions, will provide a template for me."
 
"Eclipse recognizes that I'm trying to create a test method and, if I select this first entry in the list of suggestions, will provide a template for me."
Line 39: Line 39:
 
"First, I complete the name of the test method"
 
"First, I complete the name of the test method"
  
[[Image:Tryit.gif]] Finish the method name "testDeposit".
+
*Finish the method name "testDeposit".
  
 
"I hit 'tab' and provide a body for the method."
 
"I hit 'tab' and provide a body for the method."
  
[[Image:Tryit.gif]] Hit tab. Provide a body for the method. Stop after typing "acc" (as in "account") for the second time.
+
*Hit tab. Provide a body for the method. Stop after typing "acc" (as in "account") for the second time.
  
 
"I use code completion to help me write the code by holding down the control key and hitting the space bar."  
 
"I use code completion to help me write the code by holding down the control key and hitting the space bar."  
Line 49: Line 49:
 
"Since I've declared a variable named 'account'--even though the type is unknown--Eclipse will help me by suggesting it as a code completion candidate.
 
"Since I've declared a variable named 'account'--even though the type is unknown--Eclipse will help me by suggesting it as a code completion candidate.
  
[[Image:Tryit.gif]] Continue typing. Stop after typing "Big" from "BigDecimal"
+
*Continue typing. Stop after typing "Big" from "BigDecimal"
  
 
"I know that the type 'BigDecimal' already exists, so I’ll get Eclipse to help me finish."
 
"I know that the type 'BigDecimal' already exists, so I’ll get Eclipse to help me finish."
  
[[Image:Tryit.gif]] Type it ctrl-space to complete BigDecimal.
+
*Type it ctrl-space to complete BigDecimal.
  
 
"With this completion, it actually finishes the name of the class for me, but it also adds an import statement."
 
"With this completion, it actually finishes the name of the class for me, but it also adds an import statement."
  
[[Image:Tryit.gif]]  Highlight the change in import statements.
+
* Highlight the change in import statements.
  
 
"Now I'll finish up the method."
 
"Now I'll finish up the method."
  
[[Image:Tryit.gif]] Finish typing the method.
+
*Finish typing the method.
  
 
"I want to have a second test method to test withdrawals. I'm going to simply copy the test method that I already have and change it as necessary."
 
"I want to have a second test method to test withdrawals. I'm going to simply copy the test method that I already have and change it as necessary."
  
[[Image:Tryit.gif]] Copy the method and change the code accordingly.
+
*Copy the method and change the code accordingly.
  
 
"And I save."
 
"And I save."
  
[[Image:Tryit.gif]] Click the "Save" icon.
+
*Click the "Save" icon.
  
 
"When I save a Java file, Eclipse will automatically run a build. Builds are incremental; when a Java source file is changed, that file along with any other files that reference it, are recompiled."
 
"When I save a Java file, Eclipse will automatically run a build. Builds are incremental; when a Java source file is changed, that file along with any other files that reference it, are recompiled."
Line 75: Line 75:
 
"Eclipse has discovered a number of problems with my code, shown here in the problems view.
 
"Eclipse has discovered a number of problems with my code, shown here in the problems view.
  
[[Image:Tryit.gif]] Bring the problems view to the front.
+
*Bring the problems view to the front.
  
 
This is a natural part of building code using a test-first methodology."
 
This is a natural part of building code using a test-first methodology."
Line 85: Line 85:
 
The first thing that I’m going to do is put my cursor in the word “BankAccount”. I’ll hold down the control key while I hit ‘1’. This presents me with a list of "quick fixes".
 
The first thing that I’m going to do is put my cursor in the word “BankAccount”. I’ll hold down the control key while I hit ‘1’. This presents me with a list of "quick fixes".
  
[[Image:Tryit.gif]] Hit ctrl-1
+
*Hit ctrl-1
  
 
I’ll choose to create the missing class.
 
I’ll choose to create the missing class.
  
[[Image:Tryit.gif]] Select the option to create the missing class. Click "Finish" in the wizard to accept defaults.
+
*Select the option to create the missing class. Click "Finish" in the wizard to accept defaults.
  
 
And now, back to my test class.
 
And now, back to my test class.
Line 95: Line 95:
 
It seems that I’m using a method that doesn’t exist yet. Again, quick fix can fill in the missing pieces.
 
It seems that I’m using a method that doesn’t exist yet. Again, quick fix can fill in the missing pieces.
  
[[Image:Tryit.gif]] position cursor in ‘deposit’ and use ctrl-1 to create the method
+
*position cursor in ‘deposit’ and use ctrl-1 to create the method
  
 
Again, I return to the test class.
 
Again, I return to the test class.
Line 101: Line 101:
 
Now I need to create the getBalance method. Yet again, I’ll use the quick fix.
 
Now I need to create the getBalance method. Yet again, I’ll use the quick fix.
  
[[Image:Tryit.gif]] do it.
+
*do it.
  
 
Now the testDeposit method can compile.
 
Now the testDeposit method can compile.
Line 109: Line 109:
 
I can run tests right from within the source code itself by using the context menu to select "Run as > JUnit test"
 
I can run tests right from within the source code itself by using the context menu to select "Run as > JUnit test"
  
[[Image:Tryit.gif]] do it
+
*do it
  
 
Eclipse reports that errors exist in the file and asks if I want to run the code anyway. Of course I do.
 
Eclipse reports that errors exist in the file and asks if I want to run the code anyway. Of course I do.
  
[[Image:Tryit.gif]] Click ok
+
*Click ok
  
 
This is a unique feature of Eclipse: the ability to run code with compilation errors.
 
This is a unique feature of Eclipse: the ability to run code with compilation errors.
Line 121: Line 121:
 
I can see two errors.
 
I can see two errors.
  
[[Image:Tryit.gif]] Highlight the testDeposit error
+
*Highlight the testDeposit error
  
 
This one is an assertion failed error. The test did not produce the results expected. I can solve this one by actually implementing the methods in the BankAccount class.
 
This one is an assertion failed error. The test did not produce the results expected. I can solve this one by actually implementing the methods in the BankAccount class.
Line 127: Line 127:
 
But first, notice the second error.
 
But first, notice the second error.
  
[[Image:Tryit.gif]] highlight it
+
*highlight it
  
 
This one is an “unresolved compilation problem”. This is telling me that the test could not be completed due to compilation errors.  
 
This one is an “unresolved compilation problem”. This is telling me that the test could not be completed due to compilation errors.  
  
[[Image:Tryit.gif]] highlight the testWithdraw method
+
*highlight the testWithdraw method
  
 
This is the source of the error. This method can’t compile because the withdraw method doesn’t exist. Let’s create it and rerun the test.
 
This is the source of the error. This method can’t compile because the withdraw method doesn’t exist. Let’s create it and rerun the test.
  
[[Image:Tryit.gif]] create the method and rerun the test.
+
*create the method and rerun the test.
  
 
There, now I have two assertion failed errors to deal with.  
 
There, now I have two assertion failed errors to deal with.  
Line 141: Line 141:
 
==Part 3==
 
==Part 3==
  
[[Image:Tryit.gif]] Open the BankAccount class
+
*Open the BankAccount class
  
 
I’ll make heavy use of code completion to implement the BankAccout class. First, a field.
 
I’ll make heavy use of code completion to implement the BankAccout class. First, a field.
  
[[Image:Tryit.gif]] Create the “balance” field. Type=BigDecimal, value=BigDecimal.ZERO
+
*Create the “balance” field. Type=BigDecimal, value=BigDecimal.ZERO
  
 
Notice how the code competion pops up automatically after I type a dot. If I keep typing, it just disappears. However, I’m quite lazy and instead select an appropriate entry.
 
Notice how the code competion pops up automatically after I type a dot. If I keep typing, it just disappears. However, I’m quite lazy and instead select an appropriate entry.
  
[[Image:Tryit.gif]] Implement the deposit method. Use code complete for everything
+
*Implement the deposit method. Use code complete for everything
  
 
I’ll rerun the test to see if my changes are correct.
 
I’ll rerun the test to see if my changes are correct.
  
[[Image:Tryit.gif]] Rerun the tests
+
*Rerun the tests
  
 
I have success on the testDeposit method, but still need to make the testWithdraw method work.
 
I have success on the testDeposit method, but still need to make the testWithdraw method work.
  
[[Image:Tryit.gif]] Return to the BankAccount class and implement the withdraw method. Use code completion.
+
*Return to the BankAccount class and implement the withdraw method. Use code completion.
  
 
Now I rerun my tests.
 
Now I rerun my tests.
  
[[Image:Tryit.gif]] Do it
+
*Do it
  
 
And see that everything works!
 
And see that everything works!
Line 171: Line 171:
 
First, I need to reformat this class.
 
First, I need to reformat this class.
  
[[Image:Tryit.gif]] Use “source > format” to clean up
+
*Use “source > format” to clean up
  
 
Now, I see that I have some duplication in my code. Time for some refactoring.
 
Now, I see that I have some duplication in my code. Time for some refactoring.
Line 179: Line 179:
 
First, I’m going to make this temporary variable into a field.
 
First, I’m going to make this temporary variable into a field.
  
[[Image:Tryit.gif]] Use the “local to field” refactoring
+
*Use the “local to field” refactoring
  
 
I’ll touch up the testWithdraw method to use the field.
 
I’ll touch up the testWithdraw method to use the field.
  
[[Image:Tryit.gif]] do it
+
*do it
  
 
Now, I’ll select this two lines and use the “extract method” refactoring.
 
Now, I’ll select this two lines and use the “extract method” refactoring.
  
[[Image:Tryit.gif]] do it.
+
*do it.
  
 
Notice this checkbox. Eclipse has noticed that the code fragment I’m extracting is repeated and is planning to replace the duplicate. I can turn this off if I don’t want to do this.
 
Notice this checkbox. Eclipse has noticed that the code fragment I’m extracting is repeated and is planning to replace the duplicate. I can turn this off if I don’t want to do this.
  
[[Image:Tryit.gif]] Don’t turn it off. Create a method named “setUp”
+
*Don’t turn it off. Create a method named “setUp”
  
 
This new method overrides the inherited one provided by JUnit. Eclipse warns me to make sure that we know what I’m doing. Let’s pretend that I do.
 
This new method overrides the inherited one provided by JUnit. Eclipse warns me to make sure that we know what I’m doing. Let’s pretend that I do.
  
[[Image:Tryit.gif]] accept
+
*accept
  
 
Eclipse has placed calls to the new method in place of the original code. However, JUnit calls this method for me automatically before invoking each test method, so I’m going to remove it.
 
Eclipse has placed calls to the new method in place of the original code. However, JUnit calls this method for me automatically before invoking each test method, so I’m going to remove it.
  
[[Image:Tryit.gif]] do it.
+
*do it.
  
 
I save and run.
 
I save and run.
  
[[Image:Tryit.gif]] do it
+
*do it
  
 
All is right with the world.
 
All is right with the world.

Latest revision as of 12:26, 10 April 2007

Return to Eclipse Educational Screencam Planning.

Notes

  • This demo has been recorded using an Eclipse 3.2 milestone build for the Callisto release train (link).
  • Should be updated to use JUnit 4.
  • Need to review to see if there new features that should be leveraged.

Part 1

"In this demonstration, I’m going to create a new Java application using a test first methodology."

"As you can see, I have already started Eclipse and it’s ready for me to start building a Java application."

"My first step is to create a Java project. I’ll use the new project wizard to do that."

  • Create a new project named "Banking"

"With the project created, my next step is to create a test class."

  • Create a new JUnit test class named "BankingTests".

"When I attempt to do this, Eclipse recognizes that I’m going to need access to the junit.jar library and offers to add it to my build path."

  • Provide a package "org.eclipse.banking.tests" and test class "BankingTests".

"Eclipse automatically opens the source file for this new test class."

"Now, I'll add my first test method."

  • Type "test".

"Eclipse is very helpful for lazy people like me. Having typed 'test', I now hold down the control key and hit the spacebar."

  • Type ctrl-space

"Eclipse recognizes that I'm trying to create a test method and, if I select this first entry in the list of suggestions, will provide a template for me."

"I can tab through the parts of the template."

"First, I complete the name of the test method"

  • Finish the method name "testDeposit".

"I hit 'tab' and provide a body for the method."

  • Hit tab. Provide a body for the method. Stop after typing "acc" (as in "account") for the second time.

"I use code completion to help me write the code by holding down the control key and hitting the space bar."

"Since I've declared a variable named 'account'--even though the type is unknown--Eclipse will help me by suggesting it as a code completion candidate.

  • Continue typing. Stop after typing "Big" from "BigDecimal"

"I know that the type 'BigDecimal' already exists, so I’ll get Eclipse to help me finish."

  • Type it ctrl-space to complete BigDecimal.

"With this completion, it actually finishes the name of the class for me, but it also adds an import statement."

  • Highlight the change in import statements.

"Now I'll finish up the method."

  • Finish typing the method.

"I want to have a second test method to test withdrawals. I'm going to simply copy the test method that I already have and change it as necessary."

  • Copy the method and change the code accordingly.

"And I save."

  • Click the "Save" icon.

"When I save a Java file, Eclipse will automatically run a build. Builds are incremental; when a Java source file is changed, that file along with any other files that reference it, are recompiled."

"Eclipse has discovered a number of problems with my code, shown here in the problems view.

  • Bring the problems view to the front.

This is a natural part of building code using a test-first methodology."

Part 2

Eclipse will help me fix these errors.

The first thing that I’m going to do is put my cursor in the word “BankAccount”. I’ll hold down the control key while I hit ‘1’. This presents me with a list of "quick fixes".

  • Hit ctrl-1

I’ll choose to create the missing class.

  • Select the option to create the missing class. Click "Finish" in the wizard to accept defaults.

And now, back to my test class.

It seems that I’m using a method that doesn’t exist yet. Again, quick fix can fill in the missing pieces.

  • position cursor in ‘deposit’ and use ctrl-1 to create the method

Again, I return to the test class.

Now I need to create the getBalance method. Yet again, I’ll use the quick fix.

  • do it.

Now the testDeposit method can compile.

I'll test the method.

I can run tests right from within the source code itself by using the context menu to select "Run as > JUnit test"

  • do it

Eclipse reports that errors exist in the file and asks if I want to run the code anyway. Of course I do.

  • Click ok

This is a unique feature of Eclipse: the ability to run code with compilation errors.

The JUnit view opens and reports the results of the test.

I can see two errors.

  • Highlight the testDeposit error

This one is an assertion failed error. The test did not produce the results expected. I can solve this one by actually implementing the methods in the BankAccount class.

But first, notice the second error.

  • highlight it

This one is an “unresolved compilation problem”. This is telling me that the test could not be completed due to compilation errors.

  • highlight the testWithdraw method

This is the source of the error. This method can’t compile because the withdraw method doesn’t exist. Let’s create it and rerun the test.

  • create the method and rerun the test.

There, now I have two assertion failed errors to deal with.

Part 3

  • Open the BankAccount class

I’ll make heavy use of code completion to implement the BankAccout class. First, a field.

  • Create the “balance” field. Type=BigDecimal, value=BigDecimal.ZERO

Notice how the code competion pops up automatically after I type a dot. If I keep typing, it just disappears. However, I’m quite lazy and instead select an appropriate entry.

  • Implement the deposit method. Use code complete for everything

I’ll rerun the test to see if my changes are correct.

  • Rerun the tests

I have success on the testDeposit method, but still need to make the testWithdraw method work.

  • Return to the BankAccount class and implement the withdraw method. Use code completion.

Now I rerun my tests.

  • Do it

And see that everything works!

Part 4

Time to clean up.

First, I need to reformat this class.

  • Use “source > format” to clean up

Now, I see that I have some duplication in my code. Time for some refactoring.

I’m going to create a fixture from the common code.

First, I’m going to make this temporary variable into a field.

  • Use the “local to field” refactoring

I’ll touch up the testWithdraw method to use the field.

  • do it

Now, I’ll select this two lines and use the “extract method” refactoring.

  • do it.

Notice this checkbox. Eclipse has noticed that the code fragment I’m extracting is repeated and is planning to replace the duplicate. I can turn this off if I don’t want to do this.

  • Don’t turn it off. Create a method named “setUp”

This new method overrides the inherited one provided by JUnit. Eclipse warns me to make sure that we know what I’m doing. Let’s pretend that I do.

  • accept

Eclipse has placed calls to the new method in place of the original code. However, JUnit calls this method for me automatically before invoking each test method, so I’m going to remove it.

  • do it.

I save and run.

  • do it

All is right with the world.

Part 5

TBD

Part 6

TBD

Back to the top