Testing Practice: Eclipse of JUnit (1)
Testing Practice: Eclipse of JUnit (1)
(Using JUnit With Eclipse IDE)
This article will introduce you JUnit - an engineering test mode tool. In introducing the theory of the Test-driven development, we continue to introduce "How do you create with Eclipse, JUnit Test." We will use as "hello word" as simple examples to expose to you JUnit Case.
Automated test (automated testing) in the many books being introduced, but paid little attention to how to organize stresses these tests. When the written test for a long time, it is difficult to know where to put these tests or what to call them. In Extreme Programming - Extreme Programming (xp), Test-driven development [test-driven development (TDD)] in the era of ubiquitous, it has become a big problem. You can put Test-driven development (TDD) is "Development through testing" by the development of the test.
TDD's main provisions:
L in any snippets before, we must first write this code features automatic detection procedures. Since the code does not exist, then the test failure at the outset.
L test passed, the code must be deleted from the copy.
Such an approach can be applied every programmer does not need the specific methodology. But before we start writing test, which merits our attention is, first consider how to organize automated testing.
There are several we need to consider the test
L unit testing (Unit test): These are for the inspection of individual modules (such as classes) services. If the object needs access to external data sources, such as Database, through some simulated objects (MOCK object) to simulate Database, (but only in a real environment and test data environment different times. - Such as test there's no real Datebase environment, we need MOCK Object)
L user test (Customer's test): This is the function of the system and the recognition test. All of the systems of inspection as a whole. XP in theory, these tests are prepared by the user is given test case outline.
L Integrated Test (Itegration tests): these tests, as in the user testing and unit testing between crossroads. Integration testing procedures to test several levels of interaction. , Mock Object will not appear in the set for trial, he would increase testing time. Likewise, integration testing often requires the existence of the specific testing environment, such as from the database release some test data. Integration testing may use external lib. Cactus is one such integrated J2EE lib. Explain these tests have exceeded the scope of this article, and also needs described in detail the theory, therefore, you only need to know that there will be a test.
L Development Test (Developer's test): This test is to verify that the development of the entire code, the new code increases, the additional function of the new function. For each development, a new generation at the tests to examine the code is very important. These tests organizations and the organization of these codes have the same importance.
As elsewhere in this paper, as long as that "testing" is that the development and testing (Developer's test).
In the development, a programmer may sometimes ask myself: this system acts Mody test, the existence of this test Mody, Where can I find this test? Every time the discovery of the error, are revised on the basis of the most bug rather than through automatic testing, which is a typical example. In such circumstances what progress might be:
1. To find this function tests (which may test has been written, but there is also some minor errors)
2. If such a test has not, or could not cover such testing error, we write a new test, to cover such a mistake.
3. Now we are convinced that the new testing procedures will not be adopted.
4. Repair procedures in the bug.
5. Further testing
6. Determination procedures in the test passed.
Of course, there may be a wide range of treatment, but thinking to be very clear: you can do to correct those mistakes being tested to identify.
Now, let us tell you how to deal with a developer this situation. Through the functional testing
L I use some integrated development environment (IDE) was amended to find those types and methods on what has gone wrong.
L create a known wrong environment, to find that there is an error in judgement code.
L Last but not least, written tests and put them into an existing category to the test. If you do not care to the wrong expectations of you and your colleagues can note that a copy of it and correct
All prospective ready to begin the establishment of testing, so now need to take a test name. You may say that "this is not a problem: increases in each category before a Test it!" But it is not as simple as that, let me tell you the problem if possible:
L When the time we approach in the use of TDD development, the need to test the class or method may not exist.
L may also be a good test covering a number of ways, and even several classes.
These are only the most common, which are much more.
To a test named in the proposal: First test of the name should convey this class is a test categories, and can show the exact what he wanted to check, leave this class of the flavor of the original. In fact, this very easily, please do not worry this will become the name of a very long or very ugly, how casually their own admission can be.
Below we will use the Eclipse tools to build JUnit our first test, on the assumption that you have already downloaded the current version of this product, and if not, you can keep it from the official website (www.eclipse.org). We need JUnit, you can it from the official website (www.junit.org) download and unzip to your hard disk storage java libaries place.
Eclipse open and we will build a new engineering work space (workplace project) of File -> New -> Project, select Next Java way. Input the names (project name), for example, ProjectWithJUnit. Click completed. On the establishment of such a new project, let us configurations on our Eclipse, therefore, we have JUnit library added to the build path. Click Project -> Properties, select Java Java Build Path Libraries, point Add Exteranal JARs selected JUnit.jar . You will see JUnit will appear in the list of libraries on the screen. Okay points, Eclipse will be mandatory rebuild all the build paths.
We are ready, we began to write "Hello World." Let us comply with the norms TDD: on the establishment of tests before coding. In order to, we will leave the top post will be like we were HelloWorld there is a return to the method say string ().
To the establishment of such a test in ProjectWithJUnit title right button and select New -> Other, launched the "Java", select JUnit. In the right side of the dialog box, choose a bar TestCase, and then point Next. See Figure 1.

Figure 1. Eclipse in the establishment JUnit test
In the Test class: a bar, we need to test the importation of class - HelloWorld. Test case and to take a name - for example, TestThatWeGetHelloWorldPrompt (Yes, this looks too long, but it is very clear expression of its meaning) of Finish completed.
Below are TestThatWeGetHelloWorldPrompt.java code:
Public class TestThatWeGetHelloWorldPrompt
Extends TestCase (
Public TestThatWeGetHelloWorldPrompt (
String name) (
Super (name);
)
Public void testSay () (
HelloWorld hi = new HelloWorld ();
AssertEquals ( "Hello World!" Hi.say ());
)
Public static void main (String [] args) (
Junit.textui.TestRunner.run (
TestThatWeGetHelloWorldPrompt.class);
)
)
This code is not complicated, just a little bit special. In any case, let us examine its details. We inherited the JUnit TestCase. (TestCase in the javadoc JUnit, defined as "to run multiple Test fixtures"). TestSuite JUnit also defined as a group of related TestCase composition, but we are not presented in this article. (To be continued).
Testing Practice: Eclipse of JUnit (2)
Http://www.csdn.net/develop/Read_Article.asp?Id=24223






