Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Unit and Functional Testing with
JUnit and Related Tools
Introduction
●
Testing
●
JUnit, etc.
Types of Testing
●
Unit testing: Does a single object work?
●
Integration testing: Do multiple objects work together?
●
Fu...
Why Test?
●
No-brainers: Functional, Acceptance, Performance testing
●
Why Unit Test?
– Coverage, especially of strange co...
How to use tests
●
A common testing cycle:
– Write some new code OR Fix a bug OR Refactor
some code
– Run tests to ensure ...
Basic testing principles
●
For things that can be tested automatically, tests
should be:
– easy to write
– easy to run
– o...
Writing tests
Methods of Junit.gramework.assert class
●
assertTrue(boolean)
●
You can supply any boolean expression to thi...
JUnit
●
Open source framework for testing Java code
●
Attempts to make writing unit tests easy
●
Structure:
●
TestCase [Ja...
Example
import junit.framework.TestCase;
public class TestString extends TestCase {
public void testEndsWith() {
assertTru...
Some best practices
●
TestCases (Java classes)
– Usually test a single Java class
– Name starts or ends with Test
●
Tests ...
More details
●
fixtures: setUp, tearDown
●
assertFalse, assertEquals, assertNotEquals,
assertNull, etc., etc.
●
JUnit 4/Ja...
Running JUnit tests
●
Many ways:
– Command line
– IDE (Eclipse plug-in, etc.)
– Ant
– Maven
●
Various ways of viewing outp...
Upcoming SlideShare
Loading in …5
×

0

Share

Download to read offline

Intro to junit

Download to read offline

Junit Introduction

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Intro to junit

  1. 1. Unit and Functional Testing with JUnit and Related Tools
  2. 2. Introduction ● Testing ● JUnit, etc.
  3. 3. Types of Testing ● Unit testing: Does a single object work? ● Integration testing: Do multiple objects work together? ● Functional testing: Does my application work? ● Performance testing: Does my application work well? ● Acceptance testing: Does the customer like my application?
  4. 4. Why Test? ● No-brainers: Functional, Acceptance, Performance testing ● Why Unit Test? – Coverage, especially of strange conditions – Facilitates teamwork, lessens 'ownership' – Confidence in your own work – Enable refactoring – Self-documentation of API – Improve design
  5. 5. How to use tests ● A common testing cycle: – Write some new code OR Fix a bug OR Refactor some code – Run tests to ensure the now-changed code works – Check the new code in
  6. 6. Basic testing principles ● For things that can be tested automatically, tests should be: – easy to write – easy to run – objective (easy to see if they pass or fail) – repeatable (deterministic)
  7. 7. Writing tests Methods of Junit.gramework.assert class ● assertTrue(boolean) ● You can supply any boolean expression to this method, and as long as the boolean expression evaluates to true, the method reports success. If the condition you want to check should be false, simply prepend the "!" negation operator to your parenthesized expression. ● assertTrue(String, boolean) ● Same as the first form of assertTrue, except that the supplied String is printed if the assertion fails. Most methods have a version of this form with a message string. ● assertEquals(Object, Object) ● Compares the two objects you pass in using the equals( ) method. ● assertNull(Object) ● Succeeds if the Object reference passed in is null. There is also an assertNotNull method. ● fail(String) ● Causes the test to fail, printing out the supplied String.
  8. 8. JUnit ● Open source framework for testing Java code ● Attempts to make writing unit tests easy ● Structure: ● TestCase [Java class] – Test [Java method] – Most often you write TestCase
  9. 9. Example import junit.framework.TestCase; public class TestString extends TestCase { public void testEndsWith() { assertTrue(“abcabc”.endsWith(“abc”)); } public void testEndsWithEmptyString() { assertTrue(“Doesn't end with empty string!”, “abcabc”.endsWith(“”)); } }
  10. 10. Some best practices ● TestCases (Java classes) – Usually test a single Java class – Name starts or ends with Test ● Tests (Java methods) – test a single method ● testXxx tests method xxx ● multiple tests of method xxx are named testXxxYyy, where Yyy describes a particular condition, e.g., testEndsWithEmpty ● Label your assertions ● One test per test method
  11. 11. More details ● fixtures: setUp, tearDown ● assertFalse, assertEquals, assertNotEquals, assertNull, etc., etc. ● JUnit 4/Java 5 – some incremental improvements
  12. 12. Running JUnit tests ● Many ways: – Command line – IDE (Eclipse plug-in, etc.) – Ant – Maven ● Various ways of viewing output (text, XML -> Web page, ...)

Junit Introduction

Views

Total views

136

On Slideshare

0

From embeds

0

Number of embeds

1

Actions

Downloads

3

Shares

0

Comments

0

Likes

0

×