사용자 도구

사이트 도구


java:junit

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:junit [2013/10/21 17:49]
kwon37xi [Exception Rule]
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]]
  
-===== Exception Rule ===== +===== Parameterized Test ===== 
-  * 예외 발생 여부와 발한 예외 종류를 검증할 수 있는 Rule+  * ''@RunWith(Parameterized.class)''로 지정된다. 
 +  * ''@Parameters''로 지정된 static 메소드는 배열의 컬렉션을 리턴한다. 
 +  * 파라미터 컬렉션의 배열은 테스트 클래스의 성자의 인자로 들어가게 된다. 따라서 성자 인자와 파라미터 배열의 크기가 일치해야 한다.
  
 <code java> <code java>
-  @Rule +@RunWith(Parameterized.class) 
-  public ExpectedException exception = ExpectedException.none();+class MyParameterizedClassTest { 
 +    private int multiplier;
  
-  @Test +    public MyParameterizedClassTest(int testParameter) { 
-  public void throwsIllegalArgumentExceptionIfIconIsNull() { +        this.multiplier = testParameter; 
-    // 발생할 예외에 대한 조건 기술 +    } 
-    exception.expect(IllegalArgumentException.class); + 
-    exception.expectMessage("Negative value not allowed"); +    // creates the test data 
-     + 
-    ClassToBeTested t = new ClassToBeTested(); +    @Parameterized.Parameters 
-    t.methodToBeTest(-1); +    public static java.util.Collection<Object[]> data(
-  }+        Object[][] data = new Object[][] { {1}, {5}, {121}}
 +        return Arrays.asList(data); 
 + 
 +    
 + 
 +    @Test 
 +    public void testMultiplyException() { 
 +        MyClass tester = new MyClass(); 
 +        assertEquals("Result", multiplier * multiplier, tester.multiply(multiplier, multiplier)); 
 + 
 +    } 
 +}
 </code> </code>
 +
 +===== Assume =====
 +''@Before'' 혹은 ''@Test'' 등에서 [[http://junit.sourceforge.net/javadoc/org/junit/Assume.html|Assume]]을 호출하여 테스트를 계속 실행할지 아니면 중단할지를 결정할 수 있다.
 +<code java>
 + @Before
 + public void beforeMethod() {
 +     org.junit.Assume.assumeTrue(someCondition());
 +     // rest of setup.
 + }
 +// someCondition() == true 일 때만 테스트들이 실행된다.
 +</code>
 +
 +  * [[http://www.codeaffine.com/?p=3487|A JUnit Rule to Conditionally Ignore Tests]] 조건적으로 테스트를 실행하는 ''Rule'' 예제.
 +
 +===== 테스트 실행 순서 =====
 +  * 테스트 클래스에 ''@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>
 +
 +===== Plugins =====
 +  * [[https://github.com/pholser/junit-quickcheck|jUnit QuickCheck]] 
  
java/junit.1382345355.txt.gz · 마지막으로 수정됨: 2013/10/21 17:49 저자 kwon37xi