Test Drive Development Series Part 2: Test JAVA Class with JUnit

zhaozj2021-02-17  57

Test Drive Development Series Part 2: Test JAVA Class with JUnit

Test_Driven development Series Part i: Testing Java Classes with junitby well chao

January 2004

I. Introduction (Introduction)

Seeing here you should already know why testing is so important. If you haven't yet, please read the first one in the series of papers divided into five parts. The first one is why testing is good for conceptual introduction of enterprise software. Most of the software now is hierarchical: representing layers, logic layers (business logic here) and data layers. The logical layer is the focus of the program, which includes all the rules and behaviors of the program. People often think that testing is to try a product. This is very easy to make a car: fire, start. It is also easy for desktop applications: launching programs, pointing around you, knocking on the keyboard, you can test the features you want to measure. But how do you test a JAR file consisting of a lot of Java Class?

Second, test the Java class (TESTING JAVA CLASSES)

Of course, in essence can also be started to start the Java class, then click the button. Testing a Java class is to call this class in another Java class. The following example is a Java source file. The contents of the list 1 are stored as Factorcalculator.java.

Program List 1 (factorycalcalculator.java, taken from factorycalculator.java.v1):

Import java.util.list; import java.util.ArrayList;

Public Class Factorcalculator {public int [] Factor (int Number) {list fact factorlist = new arraylist (); while (isdivisor (Number, 2)) {FactorList.Add (new integer (2)); Number / = 2;} int Upperbound = (int) math.sqrt (number) 1; for (int i = 3; i <= Upperbound; i = 2) {while (isdivisor (Number, i)) {FactorList.Add (New Integer (i )); Number / = i;}}} if (Number! = 1) {FactorList.Add (new integer (number)); Number = 1;} int [] INTARRAY = new int [Fact.Size ()]; for (INT i = 0; i

The interface provided by the FactorCalculator class is simple:

Factor: Decompose a method of obtaining a number of factors. ISPRIME: Judging a number of cases of a number. Isdivisor: Judging whether a number can be tied to another number. These public methods can constitute an API of a math library.

To test FactorCalculator, you can create a Java class with a MAIN method that can be called from the command line. The list 2 is such a test class.

Programs list 2 (CalculatorTest.java, taken from CalculatorTest.java.v1): public class CalculatorTest {public static void main (String [] argv) {FactorCalculator calc = new FactorCalculator (); int [] intArray; intArray = calc.factor (100); if (! ("== 4) && (INTARRAY [0] == 2) && (INTARRAY [1] == 2) && (INTARRAY [2] == 5) && (INTARRAY 3] == 5)) {Throw New RuntimeException ("Bad Factorization of 100");} INTARRAY = Calc.Factor (4); if (! ((INTARRAY.LENGTH == 2) && (INTARRAY [0] = = 2) && (INTARRAY [1] == 2))) {Throw new runtimeException ("Bad Factorization of 4");} INTARRAY = Calc.Factor (3); if (! (("INTARRAY.LENGTH == 1) && (INTARRAY [0] == 3))) {Throw new runtimeException ("Bad Factorization of 3");} INTARRAY = Calc.Factor (2); if (! ((INTARRAY.LENGTH == 1) && (InTARRAY [0] == 2)) {Throw New RuntimeException ("Bad Factorization Of 2");} Boolean Isprime; IsPrime = Calc.isprime (2); if (! Isprime) {throw new runtimeexc Eption ("Bad IsPrime Value for 2");} isprime = Calc.isprime (3); if (! isprime) {throw new runtimeException ("Bad Isprime Value for 3");} isprime = Calc.isprime (4); if (isPrime) {throw new RuntimeException ( "bad isPrime value for 4");} try {isPrime = calc.isPrime (1); throw new RuntimeException ( "isPrime should throw exception for numbers less than 2");

} Catch (IllegalArgumentException e) {// do nothing because throwing IAE is the proper action} boolean isDivisor; isDivisor = calc.isDivisor (6, 3); if (! IsDivisor) {throw new RuntimeException ( "bad isDivisor value for (6 , 3) ");} isdivisor = Calc.isdivisor (5, 2); if (isdivisor) {throw new runtimeException (" Bad Isdivisor Value for (5, 2) ");} Try {isdivisor = Calc.Indivisor (6 , 0); throw new RuntimeException ( "isDivisor should throw exception when potentialDivisor (the second argument) is 0");} catch (ArithmeticException e) {// do nothing because throwing AE is the proper action} System.out.println ( "All tests passed.");}} Note these two TRY-CATCH blocks, a test isprime, another test isdivisor. Sometimes throw an exception is the correct behavior of a certain code, when you test this code, you must capture this exception and see if it wants. If you throw it is not the exception you want, you should throw it to the next level of the (abnormally) processing chain. If this code should have an exception, but there is no thrown when the test is tested, you will throw your own defined exception, to notify the program function error. You should use a similar pattern similar to the JUnit test code you want to introduce below to test the original code that should throw one or more exceptions.

In order to save space, the test code above omits some content, such as what happens when the second parameter of the IsDivisor method is negative.

Compile this class with javac * .java command, then enter Java CalculaTestSt to run it.

You should get an exception when "the number of fewer numbers that cannot be judged less than 2". The general form is as follows, of course, the number of lines displayed 36 may vary because you have different ways in Calculatortest.java: Exception In Thread "Main" java.lang.RuntimeException: Isprime Should Throw Exception for NumBers Less Than 2t CalculaTest.Main (Calculatortest.java: 36)

In other words, the FactorCalculator's function is incorrect. The problem can solve this problem in the top of the isprime method, and the determination parameter is less than 2 when ILLEGALARGUMENTEXCEPTION exception is thrown. Just like a small code below: IF (Number <2) {throw new illegalgumentException ();

Place the above code to the front of the FactorCalculator's ISPRIME method. For your reference, the modified Isprime method is listed in FactorCalculator.java.v2, if you intend to use this file directly, first change it to FactorCalculator.java. After adding a check, recompile the run CalculaTest, and the new CalculaTortest should be able to pass all tests.

Third, JUnit provides the advantages of test framework (JUnit Provides Advantages As a Test Framework)

The internal function of the test Java class is the work you have made. The main difference between the true test and the simple example of just now is the size and complexity of the code base. When processing a large pile of code, you will need to collect the situation report. However, the above example has stopped the first error, which does not collect as many error messages as possible, and cannot report those tests that can be passed. If a test does not pass, you will recompile the entire test, run again, the development process will definitely be very slow. BUG is often associated with each other and is attached by each code interaction. Once I saw multiple errors, I can help you analyze and solve the bug, and the processing of associated bugs will also speed up.

Before using JUnit to rewrite this test, you need to understand the following terms and test concepts:

1. Unit test: unit test refers to a small piece of code - only one Java class in most cases - Test a software or library very limited part. The code for unit test test is small, such as one or more classes. Usually test EJB components and ordinary Java class libraries, whether these classes are run independently in the server-side (container) environment. Compared with another conceptual function test mentioned in the list, the main difference between unit test is that the focus of unit test is the internal components that end users can generally see, and the functional test focuses on "Click". In JUnit, unit testing may be a method in the TestCase class, or the entire TestCase class. From the size of the code to one or two pages to unit tests should be appropriate. If the unit test is too exaggerated, it is better to divide a few granularity more tested.

2. Functional Test: Functional test is whether the function of standing in the end user's angle verification program is correct. Functional testing and black box testing are the same meaning. 3. Black Box Test: Black box test is only based on external issued interfaces or public conventions, and ignore the inside implementation of the program. This usually means that you only know what should be entered, only test the expected output, do not know how the program generates these output, performance, boundary impact, and other external features such as the output, not in your consideration, unless these aspects of the code are public conventions a part of. If you have developed a software library available to other developers (your end users) API, black box test is particularly important. This importance refers to the software library can be done by the public interface, and the software library avoids or omitted or omitted in the public interface. If you develop a program, it is also important to comply with the public interface because it makes you and colleagues more effectively. 4, white box test: White Box Test is the function of testing a piece of code in the case where the code is implemented. When you have not specified in the public convention, but very important behaviors, such as performance issues, white box tests will be used. While testing some particularly complex algorithms or business logic, a white box test is also required. In this case, you can focus on a place where you may have an error in the black box test, which is difficult to do in the black box test. 5. In-container test: The container is tested inside the servlet or EJB container, so it can communicate more directly and the code to be tested. The Jakarta Cactus project implements a free test tool CACTUS, which makes you and the code to test in the same container, whether this container is a servlet container or an EJB container. The container test is not used for the black box function test, and its role is embodied in unit testing. 6. Test case: Test Case is a group of related tests in JUnit. Test case is the class that inherits junit.framework.testcase. Test Case usually has multiple methods, each method test program on one aspect. Test methods in Test Case are habits to name Test, but it is not necessary to do so as long as you do not have conflicts with other methods. 7. Test Suite: Test Suite is a set of Test Case or Test Suites. It is manifested as a class that inherits JUnit.Framework.TestSuite. Without any restrictions, Test Suite only includes Test Case or consisting of Test Suite, which can have both Test Case, and Test Suite. A Test Suite's child Test Suite can also include Test Case and Test Suite, so the nested test is allowed. As long as you like, you can build a few sets of Test Case, a clear small aspect of each set of test programs. A group can form a Test Suite. Then you can integrate them into a primary test suite, and finally test the entire program with this main Test Suite, a function point test. 8. Test Runner: Test Runner is a JUnit class that starts a test process. You call Test Runner, which performs your reservation in order. There are several ways to define tests to be executed by Test Runner. These methods are described in the fifth part of the fifth part "Specify the test to run".

JUnit has three different Test Runners: Text, AWT, and SWING, class names are junit.textui.teestrunner, junit.awtui.teestrunner and junit.swingui.teestrunner. 4. Write the JUnit Test (Writing a Test with Junit)

Write a JUnit test as long as the JUnit.Framework.TestCase class is OK. Your TestCase subclasses will call Test Cases in the order you want, including possible testing, and test after testing. Set in the setup method. Clear in the TEARDOWN method. You can, but not a must, overloading these two methods do what you want to do.

Below is a Test Case that rewrites the above example with JUnit:

Program list 3 (Calculatortest.java, Taken from CalculatorTest.java.v2):

Import junit.framework.testcase;

Public Class CalculaTest Extends Testcase {

PRIVATE FACTORCALCULATOR CALC;

Public CalculaTortest (String Name) {Super (Name);

Protected void setup () {CALC = New factorycalcalcalculator ();

Public void testfor () {int NumTofactor; int [] FactorRray; int [] CorrectFactorRray;

numToFactor = 100; factorArray = calc.factor (numToFactor); correctFactorArray = new int [] {2, 2, 5, 5}; assertTrue ( "bad factorization of" numToFactor, isSameFactorArray (factorArray, correctFactorArray));

numToFactor = 4; factorArray = calc.factor (numToFactor); correctFactorArray = new int [] {2, 2}; assertTrue ( "bad factorization of" numToFactor, isSameFactorArray (factorArray, correctFactorArray));

numToFactor = 3; factorArray = calc.factor (numToFactor); correctFactorArray = new int [] {3}; assertTrue ( "bad factorization of" numToFactor, isSameFactorArray (factorArray, correctFactorArray));

numToFactor = 2; factorArray = calc.factor (numToFactor); correctFactorArray = new int [] {2}; assertTrue ( "bad factorization of" numToFactor, isSameFactorArray (factorArray, correctFactorArray));}

// presumes both factor arrays are in numeric order private boolean isSameFactorArray (int [] factorArray1, int [] factorArray2) {boolean isSame = false; if (factorArray1.length == factorArray2.length) {isSame = true; for (int i = 0; i

NumToCheck = 2; isprime = Calc.isprime (NumToCheck); AssertTrue ("Bad Isprime Value for" NumToCheck, isprime);

NumToCheck = 3; isprime = Calc.isprime (NumToCheck); AssertTrue ("Bad Isprime Value for" NumToCheck, isprime);

NumToCheck = 4; isprime = Calc.isprime (NumToCheck); Assertfalse ("Bad Isprime Value for" NumToCheck, isprime);

try {numToCheck = 1; isPrime = calc.isPrime (numToCheck); fail ( "isPrime should throw exception for numbers less than 2");} catch (IllegalArgumentException e) {// do nothing because throwing IAE is the proper action}}

Public void testisdivisor () {Int NumTocheck; int potentialdivisor; boolean isdivisor;

NumToCheck = 6; potentialdivisor = 3; isdivisor = Calc.isdivisor (NumToCheck, PotentialDivisor); AssertTrue (" NumToCheck ", " PotentialDivisor ") ", ISDIVISOR);

numToCheck = 5; potentialDivisor = 2; isDivisor = calc.isDivisor (numToCheck, potentialDivisor); assertFalse ( "bad isDivisor value for (" numToCheck "," potentialDivisor ")", isDivisor); try {numToCheck = 6; potentialDivisor = 0; isDivisor = calc.isDivisor (numToCheck, potentialDivisor); fail ( "isDivisor should throw an exception when potentialDivisor is 0 but did not");} catch (ArithmeticException e) {// do nothing because throwing AE is the proper Action}}}}}

Through method name Assertxxx you can let Junit know the results you want, where xxx is true, fase, equals, or other conditions. JUnit records the pass / fail state of the Assertxxx method, and feeds to you after all the tests are executed. Here is some assertions for signature and operation description in JUnit:

Asserttrue (String ErrorMessage, Boolean BooleaneXpression): Check if the BooleaneXpression value is TRUE. If not, add ERRORMESSAGE to the list of displayed error reports. Assertfalse (String ErrorMessage, Boolean BooleaneXpression): Check if the BooleaneXpression value is false. If not, add ERRORMESSAGE to the list of displayed error reports. Assertequals (String ErrorMessage, Object A, Object B): Check if the object A is equal to object B, through the Equals method, if not, add ERRORMESSAGE to the list of displayed error reports. Object A is a desired value, the object B is the value that the program to be tested is actually returned. Assertnull (String ErrorMessage, Object O): Check if the object O is NULL. If not, add ERRORMESSAGE to the list of displayed error reports.

To see a complete list of all assertions, please refer to the Javadoc documentation (http://www.junit.org/junit/javadoc/index.htm) of the Assert class.

You can use the assertxxx statement in the entire test code to confirm that a condition result in the code you want to measure is true (or FALSE, depending on the situation).

5. Specifying the test to run (Specifying Which Tests to Run)

To run your test, you need: an instance of a Testrunner class. An instance of a test class (such as the MyTestClass class of this example), which contains the test you want to run. This class must inherit junit.framework.testcase. Tell this Testrunner instance What tests in your MyTestClass instance is to run.

Creating an instance of Testrunner and the specified MyTestClass instance is very easy, you can use the following command: Java junit.textui.teStrunner MyTestClass instead of JUnit.Textui.Testrunner with its corresponding Testrunner, such as the AWT JUnit.Awtui.Testrunner And Swing Junit.swingui.Testrunner. You have to replace myTestClass with the name of your own test class.

There are two ways to let Testrunner know which tests you have to run in the MyTestClass class. One is an explicit route, one is the default path. In MyTestClass, you can choose whether to include a public static method Suite, this method does not have any parameters, return Test objects. More specifically, it returns an object that implements Test interface, because Test is an interface, not class. Most of you use TestSuite and your own TestCase subclass, TestSuite and TestCase have implemented TEST interfaces.

If you omit the Suite method in the MyTestClass method, Testrunner finds all the method of "test" as a prefix name through the Reflection mechanism and runs them. This is the default path to notify Testrunner to run.

If you implement the Suite method in MyTestClass, Testrunner calls the Suite method, and the Test object returned by the Suite method, Testrunner learned that it is tested. This is an explicit pathway. Both TestCase and TestSuite classes implement Test interface, which means that you can only return to TestCase, or return a TestSuite that contains 0 to multiple TestCase / TestSuite, which allows multiple tests and hierarchical tests.

Specify the test to run in junit.framework.testcase

There are two ways in TestCase to specify a test method: a static dynamic. The static method is the RunTest method of Testcase, which calls your test. E.g:

import junit.framework.TestCase; public class MySimpleTest extends TestCase {public MySimpleTest (String name) {super (name);} public void runTest () {testTurnLeft ();} public void testTurnLeft () {... code here .. }

Sometimes the easiest and flexible overload TestCase.Runtest is to use an anonymous internal class. The following code describes this method: testcase testcase = new mysimpletest ("mynamefort") {public void runtest () {testTurnLNLT ();}}

Anonymous internal classes let you overload Runtest in the class of instantiation Test classes, so you can have different RUNTest implementations in different places, which use mysImpletest as the actual test method. If you initialize itself in the suite method of the Test class, this initialization Test class is its own.

The NAME parameter through the constructor can be dynamically specified in TestCase. For the MySIMPleTest class above, you can write: testcase testcase = new mysimpletest ("testturnleft");

Because you don't have to overload Runtest, the default implementation of the TestCase class will find the method TestTurnLeft by Reflection. You can replace "testturnleft" with any name you like. Six, specify multi-layer testing with junit.framework.testsuite (Specifying a hierarchy of tests to run with junit.framework.testsuite)

The TestSuite class can pack multiple tests into a group. The basic form is as follows: TestSuite testSuite = new TestSuite (); testSuite.addTest (new MySimpleTest ( "testTurnLeft")); testSuite.addTest (new CalculatorTest ( "testIsDivisor")); testSuite.addTest (new TestSuite (MyThirdTest.class)) ;

The first two addtest methods are directly called. The TestSuite.addtest method accepts objects that implement the TEST interface. The MySIMpletest and CalculaTest classes are subclats for TestCase, while TestCase implements TEST interfaces. Through the first two addTest methods, you just add two test methods to the test list to be executed by the TestSuite instance.

The third ADDTest call describes how to create a hierarchical test by including a TestSuite instance in the TestSuite instance. The TestSuite class implements the TEST interface, so you can use the parameters of the AddTest method. In the third ADDTest call, the new TestSuite object contains all Testxxx methods for the Mythirdtest class. There is no restriction requires that the TestSuite instance specified in the AddTest method is a single layer list, and the sub-testsuite can also contain sub-TestSuite.

Seven, again focusing TestSuite.suite () Method (BACK to the Testcase.suite () Method)

Now we have a test of how to specify TestCase and TestSuite to run, let us go back to see the TestCase.suite () method required for Testrunner. There is an example of a TestCase.suite () method, which adds a test method of the TestCase class, all test methods of another TestCase class, and a sub-TestSuite all level test methods.

Program list 4 (a Suite Method Demonstrating Many Different Ways of Specifying Tests):

Public static suite () {testsuite globaltestsuite = new testsuite ();

Testcase addtocarttestcase = new shopcarttest ("testaddtocart"); globaltestsuite.addtest (addtocarttestcase);

Testcase CheckoutTestCase = New ShopcartTest ("TestCheckout"); GlobalTestSuite.Addtest (CheckoutTestCase);

TestSuite Calctestsuite = New TestSuite (CalculatorTest.class); GlobalTestSuite.addTest (CALCTESTSUITE);

TestSuite fileModuleTestSuite = new TestSuite (); fileModuleTestSuite.addTest (new ImportExportTest ( "testImport")); fileModuleTestSuite.addTest (new TestSuite (SaveFileTest.class)); globalTestSuite.addTest (fileModuleTestSuite); return globalTestSuite;}

Ok, you have learned how to specify different methods to Testrunner, you should start these tests. If you add a Suite method to CalculaTest, delete it, because Testrunner in the next section will run all TestXxxx methods in the CalculaTortest class. Suite method is very important when you have to do a lot of tests.

Eight, running test (Running the test)

Enter javac -classpath ~ / packages / junit3.8.1 / junit.jar * .java compile CalculaTortest class. Use the path to the JUnit.jar file on your machine instead "~ / packages / junit3.8.1 / junit.jar". Enter java -classpath ~ / packages / junit3.8.1 / junit.jar :. Junit.Textui.Testrunner CalculaTestSt run test. The JUnit.jar path here also needs to be replaced. In order to avoid the ClassPath at the command line each time, add the JUnit library and the current directory to the classpath. LINUX you can use these two commands in Bash Shell: classpath = ~ / packages / junit3.8.1 / junit.jar: .export ClassPath

Pay attention to the correct path of "~ / packages / junit3.8.1 / junit.jar" as the JUnit.jar file, and don't forget the colon and points behind. The command to set the environment variable under Windows is "set", you can use it to set the classpath to a similar value, except that the forward slash is changed to a backslash. Putting "." Join ClassPath is to let Junit Testrunner find CalculaTortest in the current directory. For this article, you should use "." Instead of hardcoding of the current path, because you still practice other examples, so you have access to the new current directory whether you are doing that example. Here, you have set up your ClassPath correctly.

After running the test in CalculaTest, you should see the output below: ... Time: 0.008

OK (3 Tests)

A strike indicates that JUnit is running, and JUnit also shows the number of tests or failures in statistical row. If a test fails, the display result may not be the same, but:

.. FTIME: 0.01There Was 1 Failure: 1) TestAddition (TEST) "Expected: <5> But Was: <4>"

Failures !!! Tests Run: 2, Failures: 1, Errors: 0

Nine, other Testrunner class and implementation methods (Different Testrunner Classes and Ways of Executing them)

There are several Testrunners you can use: Text, AWT, and Swing. The corresponding class is JUnit.Textui.Testrunner, junit.awtui.teestrunner and junit.swingui.teestrunner. The command runnings Similar to: java junit.awtui.teStrunner CalculaTest - or -

Java junit.swingui.teestrunner CalculaTestst

The AWT and SWING version of Testrunner need to use in graphical environments such as Windows, OS X, and X11. They use the interactive graphics format to display the run results. The TEXT UI is most common, because testing is generally run in batch mode, and interaction is a disadvantage.

When you call Testrunner, pass the name of the test class to it, Testrunner loads your class, use the Reflection to find all the methods starting with "TEST". If you don't want to call the Testrunner class in the command line Java, you still have another important way: directly call the main method containing the class of Test Suite.

Enter the following as described below as a Main method of TestCase subclass: public static void main (string [] argv) {junit.textui.teStrunner.Run (Suite ());}

If you need it, use junit.awtui.teestrunner or junit.swingui.teestrunner instead of junit.textui.teStrunner.

Convenience, another version of CalculaTest.java.v3 is provided in the sample file, which contains the Suite method and the main method described above. Of course, you need to rename it to CalculaTest.java before use. If the environment you use is Linux, or Cygwin simulates UNIX under Windows, you can use the DIFF command to see the difference between different versions, which is usually useful when learning new things. For example, enter Diff CalculaTest.java.v2 CalculaTest.java.v3 can view the difference between CalculaTortest.java.v2 and CalculaTortest.java.v3.

After compiling the new CalculaTortest class, you can run. This time you don't have to enter Java Junit.textui.Testrunner CalculaTest, you can replace it with Java CalculaTest.

Ten, add functions in procedures and tests (Add FunctionAlity to your application and to your test)

Now, if you want to add a function in FactorCalculator. The development method of test driver recommends that you first increase the test, the verification test failed, (the code has not been written in course fail, the translator's note.) Then write new features, and ensure that the test passes. Now if you want to add a method of seeking the maximum number of conventions, the name is "GCD". (Omitting 20 words, translator's note) can use the following three verification: 6 and 4 The maximum number of conventions of 2.36 and 18 are the maximum number of conventions of 18.30 and 75 are 15.

Those who have just been normal. In addition, you should also test the edges and errors, such as GCD (2, 1), and GCD (3, -1). The following code can do these tests:

Program list 5:

Public void testgcd () {Assertequals ("Bad Value for GCD (6, 4)", 2, Calc.gcd (6, 4)); Assertequals ("Bad Value for GCD (36, 18)", 18, CALC. GCD (36, 18)); Assertequals ("Bad Value for GCD (30, 75)", 15, Calc.gcd (30, 75)); Assertequals ("Bad Value for GCD (2, 1)", 1, Calc.gcd (2, 1)); try {calc.gcd (3, -1); Fail ("GCD Should Throw Exception for When Either Argument IS Less Than 1);} Catch (IllegaLaRgumentExcection E) {// DO Nothing Because Throwing IAE Is The Proper Action}} Adds the code in the program list 5 to CalculaTest.java. If you don't want to knock on the keyboard, you can copy CalculaTortest.java.v4 from the sample code, change the name to CalculaTest.java.

In order to make CalculaTortest to compile, you need to add a Stub in FactorCalculator, and you call the GCD method in the CalculaTest, you must define the GCD method in the FactorCalculator. Put the following to the FactorCalculator:

Public int GCD (int A, int b) {return 1;}

If you don't want to knock on the keyboard, you can copy factorycalculator.java.v3 from the sample code, change the name to Factorcalculator.java.

Obviously, most of the GCD method returns an error result, but the development method of test driver is believed to make it mistake, then correct it, "" "" "" "" ") is the most effective development." "" ErrorS First, Then Correction of Errors " mode. Test the code and other code, it may be wrong. It is possible that your test code cannot find errors in the program. If you write a test code before developing programs, you can ensure that the test correctly discovers errors, thus reducing errors.

Enter Javac * .java compile FactorCalculator and CalculaTortest classes. Make sure the JUnit library is in ClassPath when compiling and running. Enter Java CalculaTestst. You will see the output below:

.... ftime: 0.01there Was 1 Failure: 1) Testgcd (CalculaTest) JUnit.framework.assertionFaileDEctionerror: Bad Value for GCD (6, 4) Expected: <2> But Was: <1> At CalculaTest.testgcd (CalculaTest) .java: 125) ... at CalculaTest.main (Calculatortest.java:14)

Failures !!! Tests Run: 4, Failures: 1, Errors: 0

Start testing failed, this is the result of it, it is also what we want. If it doesn't fail, it means that the test is designed or implemented. JUnit will display the information you give to it, so you should write meaningful error messages. Now fix the FactorCalculator class, let it pass the test. Delete "Return 1;" in the FactorCalculator class GCD method, replaced with the following code: Program List 6 (Functional Implementation Of GCD Method):

INT gcd = 1; int smallerint = (a 1; I -) {IF (ISDivisor (A, I) && IsDivisor (B, i)) {GCD = I; Break;}} Return GCD;

If you don't want to knock on the keyboard, you can copy factorycalculator.java.v4 from the sample code, change the name into factorycalculator.java.

Enter Javac Factorcalculator.java recompile Javac Factorcalcalcalculator. Enter Java CalculaTortest Running Test. You still get an error because the GCD method does not throw an exception because the parameter is wrong. Calling GCD (3, -1) should generate an ILLEGALARGUMENTEXCEPTION, but do not do it. Put the following code to the top of the GCD method to solve this problem.

IF ((a <1) || (B <1)) {throw new illegalgumentexception ();

Modified FactorCalcalculator is factorycalculator.java.v5 in the sample code, and you can renamed Factorcalculator.java. Recommend Factorcalcalculator.java run test. Everything is normal, the test is passed, the status report is similar:

.... TIME: 0.008

OK (4 tests)

Eleven, summary (Conclusion)

Now you know how to test the unit with JUnit, conduct a trial in your own code, you can experience the benefits of program testing.

Now prepare the next chapter of the test drive development series. The next chapter is the third part of the five parts, which will take you to test the details of the server-side EJB component in the EJB container.

Author Wellie Chao has been interested in software development since 1984 and has become an occupational programmer in 1994. He led several software projects to build enterprises. They had a deep accomplishment in Java and Perl. He published several Java books, of which "Core Java Tools" (Prentice Hall) tells the use of Ant, CVS and JUnit and other open source Java tools for extreme programming and test-driven development. He published in IBMDeveloperWorks, DEVX, THESERVERSIDE, and other places cover the topic of Java programmers in the development of enterprise software. He is honored to graduate from Harvard University, where he studies economics and computers, and he is now in New York.

The translation of this chapter I have a few words in my fight. The number of (Quality) Number Definitions and the largest declines are defined. I don't think there will be people who don't understand these two concepts to see this article. I really have more (foreigners from habit). And it is very troublesome of this basic concept, each word must be carefully considered, can not have a vulnerability, the translation is needed, I also have a lazy.

转载请注明原文地址:https://www.9cbs.com/read-29832.html

New Post(0)