-Unit Test Framework-junit

Source: Internet
Author: User
Tags assert

1.Junit annotations

The difference between Junit3 and JUNIT4. Test: White box, black box test, black box: Many times are black box, standing in the user's point of view to use the function, demand-oriented way, as long as the measurement function how to use.

White Box testing: After the code to test, test logic, time, function, and so on, open source to test, the function of a test, generally by developers to test.

JUnit testing in the enterprise generally used less, more trouble to write test cases, small, medium-sized projects generally "learn Java, to the Kay Brothers school kaige123.com" black box test, contingency performance, only Test function, use. But large projects must be white box testing, all aspects of testing, performance of these are tested, time-consuming and so on to test. Each logic block is tested for performance, such as within 300ms, if it is exceeded, it is not standard.

Strict projects to write test cases, each function should write a test case to test.

We write logic code to test: JUnit tests can be divided into a number of line tests, one way is a line, each line is basically not associated. For example, a test line in front of you is wrong, and will not affect the next line of normal.

As shown, there are two lines, two methods, the line 1 has an exception does not affect the line 2 large operation.

This is not an error, it will not throw an exception.

JUnit has a previous call and a subsequent call to @before and @after, which is called before and after the call. How to use these two things?

The previous call could be used to create an object in advance of the class that needs to be tested, open the appropriate resource, and so on, and then invoke the resources that can be used to destroy the class that has already been tested.

The code, as shown, is used before the call and after the call. Be aware that different lines, methods do not call each other, do not let them be related to each other, such as your method 1 in the Call Method 2, in the event of testing, one of the methods error, it will affect the invocation of another method.

If we have two test classes to test a logical class, can we call it run together with two test classes?

Yes, combined annotations are used to allow two classes to run together:

Is the code for the second Test class, how do we combine the two test class combinations to run them together?

First, we first create a class and then use the merge annotations @runwith (Suite.class) @SuiteClasses ({testservice.class,testservice2.class}), Code:

For example, our two test classes are called. A combination test that can run many test classes at once.

In addition, the above is used to test the correct situation, we also have to measure the situation of error, @Test (expected=usernameexception) @Test (expected=passwordexception):

For example, our two methods, after giving the wrong user name and the wrong password, successfully throw the corresponding exception, so this even test case should be correct.

Expected is specifically used to test for exceptions, and if the account password is incorrect, it is not a problem to happen.

As shown, is timed out, so will error, some programs on the running time of the program is also required, if your program does not run within the specified time to complete, the program is successful. All aspects should be considered, correctness, anomalies and so on, the running time should be tested.

Assert, both JUNIT3 and JUNIT4 have this thing. is primarily used to determine the logical value. As a method: Assert.asserttrue (logical expression), if the expression inside is true, then it is passed with JUnit test, and if the expression is false, an exception is thrown, such as:

As a result, note that this is failure rather than error, which is a logical error. There are also assert.assertfalse (logical expressions), which are used to determine if the logic is false, if it is false then JUnit test passes, if true it is failure, such as:

There are cases where you can write a sentence when you make an error, as follows:

There is also a comparison of the logical comparisons of arrays, as follows:

Where the array type can be replaced with other types, the same is the comparison method.

There are also unequal comparisons:

There are also judgments that cannot be empty:

There is also a comparison of memory addresses for two objects:

There is also a fail (), which is specifically used to throw errors, as follows:

Later, when we validate the logic of the code, we can directly use the various static methods provided in the Assert class, without having to write the code to determine whether the logic is correct.

Junit3 the inside of the assert can also be used, as long as the test class to inherit the TestCase class on the line.

Hamcrest, the jar package, is also a framework that is specifically designed for logical matching and very flexible.

Multiple conditions can be combined to judge, and assert can only judge one condition. The specific use method needs to be used in conjunction with Assertthat (Actual,matcher).

There is a allof must all be satisfied, that is, the conditions within the brackets are satisfied, such as:

As long as the inside of the EndsWith does not pass, the result of the whole judgment is wrong, there will be failure

There is also a anyof is that as long as there is a satisfaction in the condition can pass, only two are not satisfied when will not pass:

-Unit Test Framework-junit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.