官术网_书友最值得收藏!

JUnit

JUnit (http://junit.org/) is a simple and easy-to-learn framework for writing and running tests. Each test is mapped as a method, and each method should represent a specific known scenario in which a part of our code will be executed. The code verification is made by comparing the expected output or behavior with the actual output.

The following is the test class written with JUnit. There are some scenarios missing, but for now we are interested in showing what tests look like. We will focus on better ways to test our code and on best practices later in this book.

Test classes usually consist of three stages: set up, test, and tear down. Let's start with methods that set up data needed for tests. A setup can be performed on a class or method level:

Friendships friendships; 
 
@BeforeClass 
public static void beforeClass() { 
  // This method will be executed once on initialization time 
} 
 
@Before 
public void before() { 
  friendships = new Friendships(); 
  friendships.makeFriends("Joe",",," "Audrey"); 
  friendships.makeFriends("Joe", "Peter"); 
  friendships.makeFriends("Joe", "Michael"); 
  friendships.makeFriends("Joe", "Britney"); 
  friendships.makeFriends("Joe", "Paul"); 
}

The @BeforeClass annotation specifies a method that will be run once before any of the test methods in the class. It is a useful way to do some general set up that will be used by most (if not all) tests.

The @Before annotation specifies a method that will be run before each test method. We can use it to set up test data without worrying that the tests that are run afterwards will change the state of that data. In the preceding example, we're instantiating the Friendships class and adding five sample entries to the Friendships list. No matter what changes will be performed by each individual test, this data will be recreated over and over until all the tests are performed.

Common examples of usage of those two annotations are the setting up of database data, the creation of files needed for tests, and so on. Later on, we'll see how external dependencies can and should be avoided using mocks. Nevertheless, functional or integration tests might still need those dependencies and the @Before and @BeforeClass annotations are a good way to set them up.

Once the data is set up, we can proceed with the actual tests:

@Test 
public void alexDoesNotHaveFriends() { Assert.assertTrue("Alex does not have friends",
friendships.getFriendsList("Alex").isEmpty()); } @Test public void joeHas5Friends() { Assert.assertEquals("Joe has 5 friends", 5,
friendships.getFriendsList("Joe").size()); } @Test public void joeIsFriendWithEveryone() { List<String> friendsOfJoe = Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul"); Assert.assertTrue(friendships.getFriendsList("Joe")
.containsAll(friendsOfJoe)); }

In this example, we are using a few of the many different types of asserts. We're confirming that Alex does not have any friends, while Joe is a very popular guy with five friends (Audrey, Peter, Michael, Britney, and Paul).

Finally, once the tests are finished, we might need to perform some cleanup:

@AfterClass 
public static void afterClass() { 
  // This method will be executed once when all test are executed 
} 
 
@After 
public void after() { 
  // This method will be executed once after each test execution 
} 

In our example, in the Friendships class, we have no need to clean up anything. If there were such a need, those two annotations would provide that feature. They work in a similar fashion to the @Before and @BeforeClass annotations. @AfterClass is run once all tests are finished. The @After annotation is executed after each test. This runs each test method as a separate class instance. As long as we are avoiding global variables and external resources, such as databases and APIs, each test is isolated from the others. Whatever was done in one, does not affect the rest.

The complete source code can be found in the FriendshipsTest class at https://bitbucket.org/vfarcic/tdd-java-ch02-example-junit.

主站蜘蛛池模板: 旅游| 昭通市| 石景山区| 台中县| 监利县| 大竹县| 德阳市| 湟源县| 会泽县| 科尔| 高雄县| 永德县| 玛曲县| 雅安市| 静安区| 明光市| 古田县| 明光市| 永宁县| 久治县| 桂阳县| 镇平县| 安溪县| 原平市| 伊通| 临湘市| 苍南县| 丹凤县| 明溪县| 米林县| 马鞍山市| 肃南| 新巴尔虎左旗| 壶关县| 平武县| 舟曲县| 杭锦旗| 布拖县| 建昌县| 龙川县| 金沙县|