Skip to content

JUnitExample

jabiercoding edited this page Feb 3, 2016 · 2 revisions

Import the JUnitExample project in Eclipse and let's see what we have there:

  • tests is a separated source folder, this is a good practice. If we use the same package name we can access and test protected methods
  • @Test is the annotation to be included before the test method. The method can have any name you want
  • We test normal cases and those that we think that can be problematic (e.g. addition overflow)
  • Test suits (MyTestSuite.java) can group tests
  • Right click a test method, a test java file or a test suite -> Run as... -> JUnit Test
  • In the ReverseTest.java there are some commented lines. Uncomment them to see the effect of the annotations @BeforeClass @AfterClass @After @Before . Those are important when dealing with time or resource consuming tasks (e.g. database connections) that need to be done before or after running the tests
  • JUnit do not impose a way to structure your tests, you can have test classes for each class under test, or one test class for each method or other ways depending on the case
Clone this wiki locally