JUnit

JUnit 다운로드는 공식홈페이지 링크에서 받으면 된다.

Assertion

JUnit은 모든 원시 형 객체 및 배열 (기본 또는 객체)의 오버로드 된 assertion 메소드를 제공합니다. 옵션에서 첫 번째 매개 변수는 실패했을 때 출력되는 String 메시지입니다. assertThat에는 옵션의 실패 메시지의 매개 변수 실제 값과 Matcher 객체가 있습니다.

assertArrayEquals(String message,  byte[] expecteds, byte[] actuals)
assertEquals(String arg0, Object arg1, Object arg2)
assertFalse(String message, boolean condition)
assertNotNull(String message, Object object)
assertNotSame(String message, Object unexpected, Object actual)

assertNull(String message, Object object)
assertSame(String message, Object expected, Object actual)
assertThat(String actual, Matcher<? super String> matcher)
assertTrue(String message, boolean condition)

Test Runners

Using older runners with Adapter

JUnit4TestAdapter을 사용하면 JUnit-3 style 테스트 러너를 사용하여 JUnit-4 style의 테스트를 실행할 수 있습니다. 그것을 사용하려면 테스트 클래스에 다음을 추가한다.

   public static Test suite() {
       return new JUnit4TestAdapter('YourJUnit4TestClass'.class);
   }

@RunWith annotation

클래스에 @RunWith 주석을 달고 @RunWith어노테이션 클래스를 상속하면 JUnit에 포함된 작동이 아니라 그 클래스에서 테스트를 참조하기 위해 참조하는 클래스를 호출한다.

Specialized Runners

Suit

suit는 테스트를 포함한 스위트를 수동으로 빌드 할 수 있도록하는 표준 runner이다.

다음 클래스는 suit 주석의 자리 표시 자입니다. 다른 구현이 필요하지 않습니다. 이 특정 테스트 클래스를 실행하는 데 사용하는 JUnit 4 테스트 러너가 org.junit.runners.Suite임을 지정 @RunWith 주석에 주의하십시오. 이것은 @Suite 주석과 함께 작동합니다 .Suite 주석은 스위트 러너이 제품군에 어떤 테스트 클래스를 어떤 순서로 포함 여부를 표시합니다.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestFeatureLogin.class,
  TestFeatureLogout.class,
  TestFeatureNavigate.class,
  TestFeatureUpdate.class
})

public class FeatureTestSuite {
  // the class remains empty,
  // used only as a holder for the above annotations
}

Parameterized

Parameterized는 매개 변수화 된 테스트를 구현하는 표준 러너입니다. 매개 변수화 된 테스트 클래스를 실행할 때 테스트 메소드와 테스트 데이터 요소의 교차 제품에 대한 인스턴스가 작성됩니다.

사용자 Runner Parameterized은 매개 변수화 된 테스트를 구현합니다. 파라미터화 된 테스트 클래스를 실행하는 경우 테스트 방법 및 테스트 데이터 요소의 외적에 대한 인스턴스가 작성됩니다.

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
           });
    }

    private int fInput;
    private int fExpected;

    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}
public class Fibonacci {
    public static int compute(int n) {
        int result = 0;

        if (n <= 1) {
            result = n;
        } else {
            result = compute(n - 1) + compute(n - 2); 
        }

        return result;
    }
}

FibonacciTest의 각 인스턴스는 2 인자 생성자와 @Parameters 메소드의 데이터 값을 사용하여 구축됩니다.

Using @Parameter for Field injection instead of Constructor

@Parameter 주석을 사용하여 생성자를 필요로하지 않고 필드에 직접 데이터 값을 주입 할 수 있습니다.

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }  
           });
    }

    @Parameter // first data value (0) is default
    public /* NOT private */ int fInput;

    @Parameter(value = 1)
    public /* NOT private */ int fExpected;

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}

public class Fibonacci {
    ...
}

Tests with single parameter

테스트에 하나의 매개 변수 만 필요한 경우는 배열에 랩 할 필요가 없습니다. 대신 Iterable 또는 객체의 배열을 제공합니다.

@Parameters
public static Iterable<? extends Object> data() {
    return Arrays.asList("first test", "second test");
}

or

@Parameters
public static Object[] data() {
    return new Object[] { "first test", "second test" };
}

Identify Individual test cases

Parameterized 테스트에서 개별 테스트 케이스를 쉽게 식별하기 위해 @ Parameters 주석을 사용하여 이름을 지정할 수 있습니다. 이 이름에는 런타임에 대체되는 자리 표시자가 포함되어 있습니다. {index}: 현재 파라미터 index {0}, {1}, …: 첫 번째, 두 번째 등의 매개 변수 값

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest {

    @Parameters(name = "{index}: fib({0})={1}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { 
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
           });
    }

    private int input;
    private int expected;

    public FibonacciTest(int input, int expected) {
        this.input = input;
        this.expected = expected;
    }

    @Test
    public void test() {
        assertEquals(expected, Fibonacci.compute(input));
    }
}

public class Fibonacci {
    ...
}

Categories

Categories는 특정 Categories 태그 된 Test의 일부분이 특정 테스트 실행에서 실행 / 제거되도록하는 표준 입니다.

테스트 클래스에서 Categories 는 @IncludeCategory 주석으로 지정된 카테고리 또는 카테고리의 하위 유형 주석이 첨부 된 클래스 및 메서드 만 실행합니다. 클래스 또는 인터페이스 중 하나를 카테고리로 사용할 수 있습니다. 하위 유형이 사용되므로 @ IncludeCategory (SuperClass.class)라고하면, @ Category ({SubClass.class})로 표시된 테스트가 실행됩니다.

public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ }

public class A {
  @Test
  public void a() {
    fail();
  }

  @Category(SlowTests.class)
  @Test
  public void b() {
  }
}

@Category({SlowTests.class, FastTests.class})
public class B {
  @Test
  public void c() {

  }
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
  // Will run A.b and B.c, but not A.a
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
  // Will run A.b, but not A.a or B.c
}

Maven 함께 categories 사용하기

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <groups>com.example.FastTests,com.example.RegressionTests</groups>
      </configuration>
    </plugin>
  </plugins>
</build>

http://maven.apache.org/surefire/maven-failsafe-plugin/examples/junit.html https://github.com/junit-team/junit4/wiki/Categories

Experimental Runners

Enclosed

예를 들어 내부 클래스에 테스트를 놓으면 Ant는 찾을 수 없을 것입니다. Enclosed에서 외부 클래스를 실행하면 내부 클래스의 테스트가 실행됩니다. 내부 클래스에 테스트를 넣고 편의를 위해 그들을 그룹화하거나 상수를 공유 할 수 있습니다.

HashCode, Serializable, Comparable, Builders를 테스트합니다. 이것은 https://bitbucket.org/chas678/testhelpers의 testhelpers를 사용하여 쉽게 할 수 있습니다. 보통, 각 추상 Test의 서브 클래스가 필요합니다, Enclosed runner에서, 완전히 같은 test case 클래스의 정적 인 내부 클래스가 되는 것이 있습니다.

package abstractions.domain;

import java.io.Serializable;

import com.google.common.collect.ComparisonChain;

public class Address implements Serializable, Comparable<Address> {

    private static final long serialVersionUID = 1L;
    private final String address1;
    private final String city;
    private final String state;
    private final String zip;

    private Address(Builder builder) {
        this.address1 = builder.address1;
        this.city = builder.city;
        this.state = builder.state;
        this.zip = builder.zip;
    }

    public String getAddress1() {
        return address1;
    }

    public String getCity() {
        return city;
    }

    public String getState() {
        return state;
    }

    public String getZip() {
        return zip;
    }

    @Override
    public int compareTo(Address that) {
        return ComparisonChain.start().compare(this.zip, that.zip).compare(this.state, that.state)
                .compare(this.city, that.city).compare(this.address1, that.address1).result();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) { return false; }
        if (getClass() != obj.getClass()) { return false; }
        final Address that = (Address) obj;

        return com.google.common.base.Objects.equal(this.address1, that.address1)
                && com.google.common.base.Objects.equal(this.city, that.city)
                && com.google.common.base.Objects.equal(this.state, that.state)
                && com.google.common.base.Objects.equal(this.zip, that.zip);
    }

    @Override
    public int hashCode() {
        return com.google.common.base.Objects.hashCode(getAddress1(), getCity(), getCity(), getState(), getZip());
    }

    @Override
    public String toString() {
        return com.google.common.base.Objects.toStringHelper(this).addValue(getAddress1()).addValue(getCity()).addValue(getState()).addValue(getZip()).toString();
    }

    public static class Builder {

        private String address1;
        private String city;
        private String state;
        private String zip;

        public Builder address1(String address1) {
            this.address1 = address1;
            return this;
        }

        public Address build() {
            return new Address(this);
        }

        public Builder city(String city) {
            this.city = city;
            return this;
        }

        public Builder state(String state) {
            this.state = state;
            return this;
        }

        public Builder zip(String zip) {
            this.zip = zip;
            return this;
        }
    }
}

inner classes를 구현함으로써, Enclosed runner 사용해라

package abstractions.domain;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.io.Serializable;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

import testhelpers.ComparabilityTestCase;
import testhelpers.EqualsHashCodeTestCase;
import testhelpers.SerializabilityTestCase;

/**
 * The Class AddressTest.
 */
@RunWith(Enclosed.class)
public class AddressTest {

    /**
     * The Class AddressComparabilityTest.
     */
    public static class AddressComparabilityTest extends ComparabilityTestCase<Address> {

        @Override
        protected Address createEqualInstance() throws Exception {
            return new Address.Builder().address1("2802 South Havana Street").city("Aurora").state("CO").zip("80014").build();
        }

        @Override
        protected Address createGreaterInstance() throws Exception {
            return new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
        }

        @Override
        protected Address createLessInstance() throws Exception {
            return new Address.Builder().address1("14 Broad St").city("Nashua").state("NH").zip("03064").build();
        }
    }

    /**
     * The Class AddressEqualsHashCodeTest.
     */
    public static class AddressEqualsHashCodeTest extends EqualsHashCodeTestCase {

        @Override
        protected Address createInstance() throws Exception {
            return new Address.Builder().address1("2802 South Havana Street").city("Aurora").state("CO").zip("80014").build();
        }

        @Override
        protected Address createNotEqualInstance() throws Exception {
            return new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
        }
    }

    /**
     * The Class AddressSerializabilityTest.
     */
    public static class AddressSerializabilityTest extends SerializabilityTestCase {

        @Override
        protected Serializable createInstance() throws Exception {
            return new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
        }
    }

    public static class AddressMiscTest {

        private Address address;

        /**
         * Setup.
         *
         * @throws Exception the exception
         */
        @Before
        public void setUp() throws Exception {
            address = new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
        }

        /**
         * Test builder.
         */
        @Test
        public void testBuilder() {
            assertThat(address.getAddress1(), is("9839 Carlisle Boulevard NE"));
            assertThat(address.getCity(), is("Albuquerque"));
            assertThat(address.getState(), is("NM"));
            assertThat(address.getZip(), is("87110"));
        }

        @Test
        public void testToString() {
            assertThat(address.toString(), is("Address{9839 Carlisle Boulevard NE, Albuquerque, NM, 87110}"));
        }
    }
}

Third Party Runners

SpringJUnit4ClassRunner MockitoJUnitRunner HierarchicalContextRunner Avh4's Nested NitorCreation's NestedRunner

https://github.com/junit-team/junit4/wiki/Test-runners 참고

results matching ""

    No results matching ""