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"

(New page: =Notes= *This demo has been recorded using an Eclipse 3.2 milestone build for the Callisto release train ([http://www.eclipse.org/resources/resource.php?id=290 link]). *Should be updated ...)
 
Line 1: Line 1:
 +
Return to [[Eclipse Educational Screencam Planning]].
 
=Notes=
 
=Notes=
 
*This demo has been recorded using an Eclipse 3.2 milestone build for the Callisto release train ([http://www.eclipse.org/resources/resource.php?id=290 link]).  
 
*This demo has been recorded using an Eclipse 3.2 milestone build for the Callisto release train ([http://www.eclipse.org/resources/resource.php?id=290 link]).  

Revision as of 12:25, 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."

File:Tryit.gif Create a new project named "Banking"

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

File:Tryit.gif 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."

File:Tryit.gif 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."

File:Tryit.gif 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."

File:Tryit.gif 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"

File:Tryit.gif Finish the method name "testDeposit".

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

File:Tryit.gif 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.

File:Tryit.gif 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."

File:Tryit.gif 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."

File:Tryit.gif Highlight the change in import statements.

"Now I'll finish up the method."

File:Tryit.gif 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."

File:Tryit.gif Copy the method and change the code accordingly.

"And I save."

File:Tryit.gif 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.

File:Tryit.gif 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".

File:Tryit.gif Hit ctrl-1

I’ll choose to create the missing class.

File:Tryit.gif 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.

File:Tryit.gif 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.

File:Tryit.gif 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"

File:Tryit.gif do it

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

File:Tryit.gif 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.

File:Tryit.gif 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.

File:Tryit.gif highlight it

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

File:Tryit.gif 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.

File:Tryit.gif create the method and rerun the test.

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

Part 3

File:Tryit.gif Open the BankAccount class

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

File:Tryit.gif 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.

File:Tryit.gif Implement the deposit method. Use code complete for everything

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

File:Tryit.gif Rerun the tests

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

File:Tryit.gif Return to the BankAccount class and implement the withdraw method. Use code completion.

Now I rerun my tests.

File:Tryit.gif Do it

And see that everything works!

Part 4

Time to clean up.

First, I need to reformat this class.

File:Tryit.gif 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.

File:Tryit.gif Use the “local to field” refactoring

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

File:Tryit.gif do it

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

File:Tryit.gif 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.

File:Tryit.gif 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.

File:Tryit.gif 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.

File:Tryit.gif do it.

I save and run.

File:Tryit.gif do it

All is right with the world.

Part 5

TBD

Part 6

TBD

Back to the top