In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use unit testing in Java5". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use unit testing in Java5.
1. JUnit4 uses the notes in Java5 (annotation)
Before: initialization methods are executed once for each test method (note the difference from BeforeClass, which is executed once for all methods)
After: releasing resources is executed once for each test method (note the difference from AfterClass, which is executed once for all methods)
@ Test: test method, where you can test expected exceptions and timeouts
@ Test (expected=ArithmeticException.class) checks whether the method under test throws an ArithmeticException exception
@ Ignore: ignored test method
@ BeforeClass: execute only once for all tests, and must be static void
@ AfterClass: execute only once for all tests, and must be static void
The execution order of a unit test case for a JUnit4 is:
@ BeforeClass-> @ Before-> @ Test-> @ After-> @ AfterClass
The order in which each test method is called is:
@ Before-> @ Test-> @ After
2. Execution order
Random execution
@ FixMethodOrder (MethodSorters.DEFAULT)
Public class TestOrder
Execute in order of testcase name string
@ FixMethodOrder (MethodSorters.NAME_ASCENDING)
Public class TestOrder
3. Code injection Mock
The difference between @ Mocked and @ Injectable in JMokit
Public class CommonDaoTest {
@ Mock
Private MongoTemplate mongoTemplate; / / injects the mongdb mock class without accessing the library
The target class of @ InjectMocks / / injection, the test class to be implemented
Private CommonDao commonDao
@ Before
Public void setUp () {
MockitoAnnotations.initMocks (this); / / be sure to add it here, or there will be an error.
}
@ Test
Public void dropCollectionName () {
DoNothing () .when (mongoTemplate) .dropCollection
CommonDao.dropCollectionName / / CommonDao implements the function of deleting documents
}
}
Org.jmockit
Jmockit
1.23
Test
4. Various mock examples
When (systemConfigurationService.getConfiguration (eq (String.class), anyString () .thenReturn ("(\\ d+\\. *\\ d*)")
When (objService.getKnowledgePointList (anyList () .thenReturn (objectiveKnowledges)
When (mongoTemplate.updateFirst (any (Query.class), any (Update.class), anyString ()) .thenReturn (mock (WriteResult.class))
When (mongoTemplate.collectionExists ("111")) .thenReturn (false)
When (configRepository.findByType (anyString () .thenReturn (mock (Config.class))
At this point, I believe you have a deeper understanding of "the use of unit testing in Java5". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.