JUnit Annotations and Hamcrest

Source: Internet
Author: User
Tags set time


Black box testing: black-box testing is also known as functional testing, and it is tested to see if each feature is working properly. In the test, the program as a black box can not be opened, regardless of the internal structure of the program and internal characteristics of the case, in the program interface testing, it only checks whether the program function in accordance with the requirements of the specification of the normal use, the program can properly receive input data and produce the correct output information. Black box testing focuses on the program "Learning Java, to the kaige123.com" external structure, regardless of the internal logic structure, mainly for the software interface and software function testing. The black box test simply doesn't need to know the code structure of the program, it just needs to be used like a user. So the black box test is based on the user's point of view, from the input data and output data of the corresponding relationship between the test. Obviously, if the external characteristics of the design of the problem or specification is wrong, the black box test method is not found.

white box test: White box test [1]  also known as structural testing, transparent box testing, logic-driven testing, or code-based testing. White box testing is a test case design method, the box refers to the software being tested, the white box refers to the box is visible, you know what the inside of the box and how it works. The White box method thoroughly understands the internal logic structure of the program and tests all logical paths. The "white Box" method is a poor lift path test. When using this scenario, the tester must examine the internal structure of the program, starting with the logic of the checker and drawing the test data. The number of independent paths through the program is astronomical. What is the method of testing the software? There are two kinds of common software testing methods: Static test method and dynamic test method. The static test of the software does not require the actual execution of the program on the computer, mainly with some artificial simulation technology to analyze and test the software; The dynamic test of software is to run the program dynamically by inputting a set of instance data which is constructed beforehand according to certain test criteria, and the process of discovering the program error is achieved. In the dynamic analysis technology, the most important technology is path and branch testing. The six coverage test methods described below are dynamic analysis methods. The test methods of white box testing include code checking, static structure analysis, static quality measurement, logic covering, basic path testing, domain testing, symbolic testing, path coverage and program variation. The coverage standard of the White box test method is logic overlay, loop overlay and basic path test. The logic overrides include statement overlay, decision overlay, conditional override, decision/condition overlay, conditional combination override, and path overlay. Six coverage criteria the ability to find errors is changed from weak to strong: 1. Statement overrides each statement is executed at least once. 2. Determine that each branch that overrides each decision is executed at least once. 3. The condition overrides each of the criteria for each decision to be taken to a variety of possible values. 4. The decision/condition coverage also satisfies the coverage of the decision coverage condition. 5. The combination of conditions covers at least one occurrence of each of the conditions in each decision. 6. Path overrides make every possible path in the program execute at least once.

From the above, white box testing for testers, the technical requirements are high, generally only relatively strict large projects will use white box testing, white box testing costs are also large, so generally most of the case is the programmer himself to carry out a simple white box test.

JUnit Annotations: We all know that JUNIT4 uses annotations to test methods, and these annotations are tested separately, like threads, each adding an annotation to a method, like opening a thread to test the method, so that one annotation is a branch, multiple annotations are multiple branches:

@Before and @after Annotations: The role of @Before is to invoke methods written with @before annotations prior to junit testing. The effect of the @After is reversed by invoking a method written with @after annotations at the end of the JUnit test. One thing to note is that the test described here refers to every method that writes @test annotations, that is, each branch is a JUnit test, and each branch test invokes a method written with a @before annotation before it starts, and ends with the method of calling @after annotations, for example, I wrote 3 Tests, It will call the method written with @before 3 times, call the method written with @after 3 times. code example:

Operation Result:

Adding expected in the @test annotation is used to test whether the method throws an exception that should be thrown, that is, to test the exception, and if the exception is not thrown, it means that the method is problematic, and JUnit's test results report an error. Error code example:

Operation Result:

The correct code example:

Operation Result:

Adding a timeout to the @test annotation is for the test method to run longer than the set time, and if it does not, the result of the test is correct, and the natural test result is wrong when it is timed out. Error code example:

Operation Result:

Correct code example:

Operation Result:

@RunWith and @suiteclasses are used to run multiple test classes uniformly, and the classes written with the test annotations are added to the @suiteclasses, so they can be run uniformly. This process only needs to run classes written with @runwith and @suiteclasses, and the test class does not need to run.

code example:

Operation Result:

Assert:assert is a tool class in JUnit, and the methods inside this class can give us the same effect as a judgment statement. Asserttrue/assertfalse Method:

Assertarrayequals Method:

This method is used to compare the two array objects are consistent, this method has a number of parameter types, you can compare the 8 basic data types of arrays and object type array, but also in the method parameter can be added a string, the string can be printed when the test results error, Here's an example with a Boolean array:

Operation Result:

Assertequals Method:

This method is mainly used to compare the two variables are the same, the same can compare the 8 basic data types of variables and object array objects, but also can be added to the parameters of the string, the following is an int type and an object array to do an example:

Operation Result:

Assertnotequals Method:

This method is used to compare two variables "Learn Java, to the Kaige123.com school" is not the same, not the same problem, the same will be error, you can compare long, double, float basic data type and object Type objects, Similarly, you can add a string to the argument, and the following is an example of a long type and an object type:

Operation Result:

Assertnotnull Method:

This method is used to determine whether an object of type objects is not empty, is not empty, the error is empty, the same can be added to a string in the argument. code example:

Operation Result:

Assertsame method: This method is used to match the memory address of two object objects, and the inconsistency will be error:

Fail method: This method is specifically used to output the error message, as long as the method is written, regardless of whether the test case itself is correct, the result of the test is wrong:

The method in the Hamcrest:hamcrest package allows the assert to be upgraded to a multi-branch-like judgment statement like if Esle, which typically downloads more than JUNIT4 versions of the package.

Core anything-always match if you don't care what the object under test is useful describedas-add a custom failure statement adorner is-improved readability adorner-see below "Sugar" logic AllOf-if all matches match, s Hort circuits (a word that is hard to understand, transliteration is short, it feels wrong, there is no translation) (like Java &&) anyOf-if any match matches, short circuits (like Java | |) Not-if the package matches Equalto-Test object equality using the Object.Equals method hastostring-Test object.tostring method instanceof, Iscompatibletype-Test Type N Otnullvalue, nullvalue-Test null Sameinstance-Test object instance Beans hasproperty-Test JavaBeans Property Collection array-tests an array element test an array ' s Elements against an array of matchers hasentry, Haskey, HasValue-Test a map containing an entity, key or value Hasitem, Hasitems-Test a collection containing an element ha Siteminarray-Test An array containing an element number Closeto-Test floating-point value close to a given value GreaterThan, Greaterthanorequalto, LessThan, Lessthanorequalto-Test order Text Equaltoignoringcase-Test string equality ignores case equaltoignoringwhitespace-test string ignores whitespace containsstring, EndsWith, StartsWith-Test string horse With

' Assert.assertthat ("expression not judged by", True, is (true)); Assert.assertthat ("Expression judgment does not pass", "ASD", Isnull.notnullvalue ()); Assert.assertthat ("Expression judgment does not pass", "Hello.java", AllOf (EndsWith (". Java"), StartsWith ("Hello"))); Assert.assertthat ("Expression judgment does not pass", "Hello.java", AnyOf (EndsWith (". Java"), StartsWith ("Hello")));

code example:

JUnit Annotations and Hamcrest

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.