Presentation is loading. Please wait.

Presentation is loading. Please wait.

JUnit Testing Why we do this and how we can get better.

Similar presentations


Presentation on theme: "JUnit Testing Why we do this and how we can get better."— Presentation transcript:

1 JUnit Testing Why we do this and how we can get better

2 What is JUnit JUnit testing is an extension to Java JUnit testing is an extension to Java It is a very powerful way to unit test our projects It is a very powerful way to unit test our projects Unit testing allows us to test the individuals pieces of our code Unit testing allows us to test the individuals pieces of our code At the method level, the class level and even the package level At the method level, the class level and even the package level

3 Why do we do this We use JUnit testing so we can perform regression testing. We use JUnit testing so we can perform regression testing. Regression testing is retesting after a change has been made. Regression testing is retesting after a change has been made. It is not uncommon to fix a bug in your code only to have more bugs show up. It is not uncommon to fix a bug in your code only to have more bugs show up. With regression testing and JUnit we have an easy way to specify tests and run and rerun them as we modify our code. With regression testing and JUnit we have an easy way to specify tests and run and rerun them as we modify our code.

4 How to go about testing It is true in fact that you can set your test classes up before you even write your other classes. It is true in fact that you can set your test classes up before you even write your other classes. You should be able to read the API and get a picture of what the classes are supposed to do. You should be able to read the API and get a picture of what the classes are supposed to do. You should then be able to write tests that will succeed if the methods behave correctly and fail otherwise. You should then be able to write tests that will succeed if the methods behave correctly and fail otherwise.

5 Writing a test class A JUnit test class is an actual Java class. A JUnit test class is an actual Java class. It has a couple of extra pieces, but BlueJ puts all the extra stuff for you. It has a couple of extra pieces, but BlueJ puts all the extra stuff for you. A test class is a special kind of class, it is a JUnit.framework.TestClass class. A test class is a special kind of class, it is a JUnit.framework.TestClass class. You can see this in BlueJ. You can see this in BlueJ. Because a test class is one of these special classes it has access to a lot of methods. Because a test class is one of these special classes it has access to a lot of methods.

6 Helpful methods These test classes have a lot of different methods that they can use. These test classes have a lot of different methods that they can use. The method all look more or less the same, they simply differ by the parameters. The method all look more or less the same, they simply differ by the parameters. We know this as overloading We know this as overloading The most common method is: assertEquals The most common method is: assertEquals Which says the two parameters are equal Which says the two parameters are equal

7 AssertEquals AssertEquals comes in about as many different flavors as there are types AssertEquals comes in about as many different flavors as there are types It can compare Strings, ints, double, objects, etc. It can compare Strings, ints, double, objects, etc. This is the method we use most often This is the method we use most often

8 Other Methods AssertNull AssertNull AssertNotNull AssertNotNull AssertNotEquals AssertNotEquals AssertSame AssertSame AssertTrue AssertTrue AssertFalse AssertFalse

9 Writing test classes You can and should actually open up and take a look at the test classes you write You can and should actually open up and take a look at the test classes you write Even if you write them using the BlueJ interface. Even if you write them using the BlueJ interface. There will be times when you cannot get what you want accomplished using the BlueJ interface and you will have to open the file. There will be times when you cannot get what you want accomplished using the BlueJ interface and you will have to open the file. You will want to get comfortable with what is in those files. You will want to get comfortable with what is in those files.

10 References You can find a lot of information on the web about Junit testing You can find a lot of information on the web about Junit testing Their website can be found at: http://www.junit.org Their website can be found at: http://www.junit.org http://www.junit.org There documentation about their classes can be found: http://www.junit.org/junit/javadoc/3.8.1/i ndex.htm There documentation about their classes can be found: http://www.junit.org/junit/javadoc/3.8.1/i ndex.htm http://www.junit.org/junit/javadoc/3.8.1/i ndex.htm http://www.junit.org/junit/javadoc/3.8.1/i ndex.htm


Download ppt "JUnit Testing Why we do this and how we can get better."

Similar presentations


Ads by Google