사용자 도구

사이트 도구


java:junit

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:junit [2013/11/14 15:28]
kwon37xi
java:junit [2015/11/21 13:26] (현재)
kwon37xi
줄 3: 줄 3:
   * [[http://www.javacodegeeks.com/2012/10/enhancing-spring-test-framework-with.html|Enhancing Spring Test Framework with beforeClass and afterClass setup]]   * [[http://www.javacodegeeks.com/2012/10/enhancing-spring-test-framework-with.html|Enhancing Spring Test Framework with beforeClass and afterClass setup]]
   * [[http://www.vogella.com/articles/JUnit/article.html|JUnit tutorial]]   * [[http://www.vogella.com/articles/JUnit/article.html|JUnit tutorial]]
 +  * [[http://www.javacodegeeks.com/2014/11/junit-tutorial-unit-testing.html|JUnit Tutorial Unit Testing]]
  
-==== Parameterized Test ====+===== Parameterized Test =====
   * ''@RunWith(Parameterized.class)''로 지정된다.   * ''@RunWith(Parameterized.class)''로 지정된다.
   * ''@Parameters''로 지정된 static 메소드는 배열의 컬렉션을 리턴한다.   * ''@Parameters''로 지정된 static 메소드는 배열의 컬렉션을 리턴한다.
줄 12: 줄 13:
 @RunWith(Parameterized.class) @RunWith(Parameterized.class)
 class MyParameterizedClassTest { class MyParameterizedClassTest {
- private int multiplier;+    private int multiplier;
  
- public MyParameterizedClassTest(int testParameter) { +    public MyParameterizedClassTest(int testParameter) { 
- this.multiplier = testParameter; +        this.multiplier = testParameter; 
- }+    }
  
- // creates the test data+    // creates the test data
  
- @Parameters +    @Parameterized.Parameters 
- public static java.util.Collection<Object[]> data() { +    public static java.util.Collection<Object[]> data() { 
- Object[][] data = new Object[][] { {1}, {5}, {121}}; +        Object[][] data = new Object[][] { {1}, {5}, {121}}; 
- return Arrays.asList(data);+        return Arrays.asList(data);
  
- }+    }
  
- @Test +    @Test 
- public void testMultiplyException() { +    public void testMultiplyException() { 
- MyClass tester = new MyClass(); +        MyClass tester = new MyClass(); 
- assertEquals("Result", multiplier * multiplier, tester.multiply(multiplier, multiplier));+        assertEquals("Result", multiplier * multiplier, tester.multiply(multiplier, multiplier));
  
- }+    }
 } }
 </code> </code>
  
-===== Exception Rule ===== +===== Assume ===== 
-  * 예외 발생 부와 발생한 예외 종류를 검증할 수 있는 Rule +''@Before'' 혹은 ''@Test'' 등에서 [[http://junit.sourceforge.net/javadoc/org/junit/Assume.html|Assume]]을 호출하여 테스트를 계속 실행할지 아니면 중단할지를 결정할 수 있다.
 <code java> <code java>
-  @Rule + @Before 
-  public ExpectedException exception = ExpectedException.none(); + public void beforeMethod() { 
- +     org.junit.Assume.assumeTrue(someCondition()); 
-  @Test +     // rest of setup. 
-  public void throwsIllegalArgumentExceptionIfIconIsNull() { + } 
-    // 발생할 예외에 대한 조건 기술 +// someCondition() == true 일 때만 테스트들이 실행된다.
-    exception.expect(IllegalArgumentException.class); +
-    exception.expectMessage("Negative value not allowed"); +
-     +
-    ClassToBeTested t = new ClassToBeTested()+
-    t.methodToBeTest(-1); +
-  }+
 </code> </code>
  
-===== 임시 폴더 Rule ===== +  * [[http://www.codeaffine.com/?p=3487|A JUnit Rule to Conditionally Ignore Tests]] 조건적으로 테스트를 실행하는 ''Rule'' 예제.
-  * 테스트 별로 임시 폴더를 생성한다. +
-<code java> +
-  @Rule +
-  public TemporaryFolder folder = new TemporaryFolder(); +
-   +
-  // ...+
  
-  File createdFolder folder.newFolder("newfolder"); +===== 테스트 실행 순서 ===== 
-  File createdFile = folder.newFile("myfilefile.txt");  +  * 테스트 클래스에 ''@FixMethodOrder(MethodSorters.NAME_ASCENDING)'' 형태로 설정하여 테스트 메소드 실행 순서를 보정할 수 있다. 
 +  * [[http://examples.javacodegeeks.com/core-java/junit/junit-test-order-example/|JUnit Test Order Example]] 
 + 
 +===== IntelliJ Live Template for try/catch JUnit/Hamcrest===== 
 +  * abbrevication : ''trytestjunit'' 
 +  * ''Reformat according to Style'' : check 
 +  * ''Use static import if possible'' : check 
 +  * ''Shorten FQ names'' : check 
 +  * ''Applicable in Java: statement'' 
 +  * Template text : <code java> 
 +try { 
 +    org.junit.Assert.fail("Must throw an exception"); 
 +} catch ($EXCEPTION$ ex) { 
 +    org.junit.Assert.assertThat("Must throw an exception", 
 + ex.getMessage(), org.hamcrest.CoreMatchers.is("$MESSAGE$")); 
 +}
 </code> </code>
  
 +===== Plugins =====
 +  * [[https://github.com/pholser/junit-quickcheck|jUnit QuickCheck]] 
  
java/junit.1384410484.txt.gz · 마지막으로 수정됨: 2013/11/14 15:28 저자 kwon37xi