Introduction to JUnit unit testing

@H_ 403_ 2 @ What is unit testing

Write a class to be used by others. Will there be a bug? What should I do? Test it.

How about testing with the main method? No!

Cannot run together!

In most cases, it is necessary to observe the output artificially to determine whether it is correct

@H_ 403_ 2 @ Why unit testing

Reuse tests to cope with future implementation changes.

Improve morale and know clearly that my things are OK.

@H_ 403_ 2@JUnit4 HelloWorld

JUnit and hamcrest packages need to be imported

New project establishes a class, establishes testcase assertthat, and uses the matching method of hamcrest

@H_ 403_ 2 @ discard old assertions and use hamcrest assertions

@H_ 403_ 2@a )

assertThat( n,allOf( greaterThan(1),lessThan(15) ) ); assertThat( n,anyOf( greaterThan(16),lessThan(8) ) ); assertThat( n,anything() ); assertThat( str,is( "bjsxt" ) ); assertThat( str,not( "bjxxt" ) );

@H_ 403_ 2@b )

assertThat( str,containsString( "bjsxt" ) ); assertThat( str,endsWith("bjsxt" ) ); assertThat( str,startsWith( "bjsxt" ) ); assertThat( n,equalTo( nExpected ) ); assertThat( str,equalToIgnoringCase( "bjsxt" ) ); assertThat( str,equalToIgnoringWhiteSpace( "bjsxt" ) );

@H_ 403_ 2@c )

assertThat( d,closeTo( 3.0,0.3 ) ); assertThat( d,greaterThan(3.0) ); assertThat( d,lessThan (10.0) ); assertThat( d,greaterThanOrEqualTo (5.0) ); assertThat( d,lessThanOrEqualTo (16.0) );

@H_ 403_ 2@d )

assertThat( map,hasEntry( "bjsxt","bjsxt" ) ); assertThat( iterable,hasItem ( "bjsxt" ) ); assertThat( map,hasKey ( "bjsxt" ) ); assertThat( map,hasValue ( "bjsxt" ) );

@H_ 403_ 2@Failure And error

Failure refers to the test failure, and error refers to the error of the test program itself

@H_ 403_ 2@JUnit4 Annotation

@Test: Test Method

a) (expected=XXException.class)

b) (timeout=xxx)

2. @ ignore: ignored test method

3. @ before: run before each test method

4. @ after: run after each test method

5. @ beforeclass: run before all tests start

6. @ afterclass: run after all tests are completed

@H_ 403_ 2 @ run multiple tests

@H_ 403_ 2 @ attention

Observe the agreement, such as:

a) Class in test package

b) Class names end with xxxtest

c) Method is named with testmethod

@H_ 403_ 2 @ other frames

TestNG

The above must see article for the introduction to JUnit unit testing is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>