“Arrange-Act-Assert” a pattern for arranging code in UnitTest

Each method should group code into these functional sections, separated by blank lines:

Arrange all necessary preconditions and inputs.
Act on the object or method under test.
Assert that the expected results have occurred.

Examples

@Test
 public void test() {
    String input = "abc";
    		
    String result = Util.reverse(input);
    
    assertEquals("cba", result);
 }

Benefits

  • Clearly separates what is being tested from the setup and verification steps.
  • Clarifies and focuses attention on a historically successful and generally necessary set of test steps.
  • Makes some Test-Smells more obvious
  • Assertions intermixed with “Act” code.
  • Prevent Test Methods that try to test too many different things at once.

Reference

http://wiki.c2.com/?AssembleActivateAssert

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *