Скачать презентацию JUnit 1 Unit Testing with JUnit Скачать презентацию JUnit 1 Unit Testing with JUnit

e6f95d6cf8b559a78104663ad5960159.ppt

  • Количество слайдов: 22

JUnit 1 JUnit 1

Unit Testing with JUnit • If code has no automated test case written for Unit Testing with JUnit • If code has no automated test case written for it to prove that it works, it must be assumed not to work. • An API that does not have an automated test case to show it works must be assumed unmaintainable. • Software without automated test cases cannot be economically refactored. Software that cannot be refactored cannot be extended 2

Don't have the time for unit test? • Productivity is directly related to the Don't have the time for unit test? • Productivity is directly related to the stability of your code base. – The fewer test cases you write, the less stable your code base becomes. – You spend all your time fixing unintended side effects and bugs. 3

Where to Get JUnit • JUnit was written by Erich Gamma and Kent Beck. Where to Get JUnit • JUnit was written by Erich Gamma and Kent Beck. • http: //www. junit. org/index. htm 4

System. out. println is Not Enough • It is hard to tell if a System. out. println is Not Enough • It is hard to tell if a complex system is working because so many System. out. println methods are printing so much garbage. • Second, you must determine if something works by looking at a String scrolling by on a console. The string of text scrolling by make sense the day you wrote it, but will it still make sense in three months? • Third, when you make changes, things 5

Overview of JUnit • JUnit is a framework for writing unit tests. • A Overview of JUnit • JUnit is a framework for writing unit tests. • A test case defines a fixture to run a related set of tests. Typically, every class that you write should have a test case. • A test fixture provides resources: primitive variables and objects that tests need to run. • A test suite is a collection of related 6

Practice • 1. Subclass junit. framework. Test. Case. • 2. If we need fixture Practice • 1. Subclass junit. framework. Test. Case. • 2. If we need fixture objects, override the set. Up() method. • 3. Define a number of tests that return void and whose method name begins with test, such as test. Add(), test. Put(), and test. Iterator(). • 4. If we need to release resources that were part of the fixture, override the tear. Down() method. • 5. If we need to group a related set of test 7

An example package xptoolkit. junit. example; import junit. framework. *; import java. util. Map; An example package xptoolkit. junit. example; import junit. framework. *; import java. util. Map; import java. util. Hash. Map; import junit. extensions. *; public class Hash. Map. Test extends Test. Case { private Map test. Map; private Map test. Map 2; public Hash. Map. Test(String name) { super(name); } public static Test suite() { return new Test. Suite(Hash. Map. Test. class); } 8

public static void main (String[] args) { junit. textui. Test. Runner. run (suite()); } public static void main (String[] args) { junit. textui. Test. Runner. run (suite()); } private static final String APPLE_KEY = "Apple. CEO"; private static final String APPLE_VALUE = "Apple. CEO"; protected void set. Up() { test. Map = new Hash. Map(); test. Map. put(APPLE_KEY, APPLE_VALUE); test. Map. put("Oracle. CEO", "Larry Ellison"); test. Map 2 = new Hash. Map(); test. Map 2. put("1", "1"); test. Map 2. put("2", "2"); } 9

public void test. Put(){ String key = public void test. Put(){ String key = "Employee"; String value = "Rick Hightower"; //put the value in test. Map. put(key, value); //read the value back out String value 2 = (String)test. Map. get(key); assert. Equals("The value back from the map ", value 2); } public void test. Size(){ assert. Equals (2, test. Map. size()); } public void test. Get(){ assert. Equals(APPLE_VALUE, test. Map. get(APPLE_KEY)); assert. Null(test. Map. get("JUNK_KEY")); } 10

public void test. Put. All(){ test. Map. put. All(test. Map 2); assert. Equals (4, public void test. Put. All(){ test. Map. put. All(test. Map 2); assert. Equals (4, test. Map. size()); assert. Equals("1", test. Map. get("1")); test. Get(); } public void test. Contains. Key(){ assert("It should contain the apple key", test. Map. contains. Key(APPLE_KEY)); } public void test. Contains. Value(){ assert(test. Map. contains. Key(APPLE_VALUE)); } 11

public void test. Remove(){ String key = public void test. Remove(){ String key = "Employee"; String value = "Rick Hightower"; //put the value in test. Map. put(key, value); //remove it test. Map. remove(key); //try to read the value back out assert. Null(test. Map. get(key)); } } 12

Explanation 1 • Step 1 is to define a class that derives junit. framework. Explanation 1 • Step 1 is to define a class that derives junit. framework. import junit. framework. *; . . . public class Hash. Map. Test extends Test. Case { 13

Explanation 2 • Next, if our test case needs a fixture, we override the Explanation 2 • Next, if our test case needs a fixture, we override the set. Up() method protected void set. Up() { test. Map = new Hash. Map(); test. Map. put(APPLE_KEY, APPLE_VALUE); the fixture the test case sets test. Map. put("Oracle. CEO", " up is actually instances of the Larry Ellison"); class under test: the Hash. Map class. Garbage-collection will test. Map 2 = new destroy the object. Hash. Map(); test. Map 2. put("1", "1"); test. Map 2. put("2", "2"); } 14

Explanation 3 • The Hash. Map. Test class defines several tests to test the Explanation 3 • The Hash. Map. Test class defines several tests to test the Hash. Map class. • The JUnit framework uses reflection to look for methods whose names begin with test and uses them as test cases. 15

Explanation 4 • It does this when we invoke the Test. Suite constructor in Explanation 4 • It does this when we invoke the Test. Suite constructor in the static suite() method, public static Test suite() { return new Test. Suite(Hash. Map. Test. class); } the composite design pattern 16

Explanation 5 • assert. Equals() • assert. True() 17 Explanation 5 • assert. Equals() • assert. True() 17

Explanation 6 • Note that the set. Up() and tear. Down() methods are called Explanation 6 • Note that the set. Up() and tear. Down() methods are called before and after every text. X() method that is run. • Because the set. Up() method does not allocate any resources that need to be released, the Hash. Map. Test does not need to override the tear. Down() method. 18

JUnit and Ant • Copy junit. jar to Ant’s lib directory. <project name= JUnit and Ant • Copy junit. jar to Ant’s lib directory. 19

" src="https://present5.com/presentation/e6f95d6cf8b559a78104663ad5960159/image-20.jpg" alt=" " /> 20

21 21

Reading work • http: //junit. sourceforge. net/doc/cook book/cookbook. htm 22 Reading work • http: //junit. sourceforge. net/doc/cook book/cookbook. htm 22