Preliminary understanding of Junit and assert

A beginner, gradually learning the process of java, some of his own understanding, I hope you guys can give advice.


I was suddenly asked about junit-related knowledge during an interview. I was blinded. After I came back, I made some understanding and made the following notes.


1. Eclipse imports Junit package.


First, an additional Junit factory can be added to the factory. Properties ---> java build Path ---> add library ---> junit.
Or directly create a class, and then will automatically guide the package. new ---> others ---> junit case.

2. J unit test specification

1. The test method must be modified with @Test
 2. The test method must be modified with public void, without any parameters
 3. Create a new source code directory to store our test code
 4. The test package should be consistent with the tested class
 5. Each method in the test unit must be able to be tested independently without any dependence between the tests
 6. The test class uses Test as the suffix of the class name. (Not required)
 7. The test method uses test as the suffix of the method name. (Not required)

3. Common notes of Junit

@Test: Modify a common method into a test method
@Test (expected = XX.class) @Test (timeout = ms)
 ---> Used to test endless loop, performance test
@BeforeClass: It will be executed before all methods run, static decoration
@AfterClass: It will be executed after all methods are run, static decoration
@Before: will be executed once before each test method is run
@After: will be executed once after each test method is run
@Ignore: The modified test method will be ignored by the test runner
@RunWith: You can change the test runner org.junit.runner.Runner
,
4. assert (usage of assert)

The assertion I understand is a situation prediction. If the prediction is correct, the program runs normally. If the program runs incorrectly, the program throws an exception.
assertEquals (exception, expression);
assertTure(boolean);
Need to guide the package import static org.junit.Assert. *;

Published 25 original articles · praised 0 · visits 9939

Guess you like

Origin blog.csdn.net/weixin_38246518/article/details/78651962