programing

JUnit과 Hamcrest를 함께 사용하는 방법은?

javaba 2023. 1. 29. 20:22
반응형

JUnit과 Hamcrest를 함께 사용하는 방법은?

JUnit 4.8이 Hamcrest Matchers와 어떻게 작업해야 하는지 이해할 수 없습니다.내부에 정의되어 있는 몇 가지 투수가 있습니다.org.hamcrest.CoreMatchers동시에 다른 학생들도 있습니다.org.hamcrest.Matchers어디로 갈 건데?프로젝트에 햄크레스트 JAR을 명시적으로 포함시키고 JUnit에서 제공하는 Matchers를 무시해야 합니까?

특히 제가 관심 있는 건empty()이 항아리에서는 찾을 수 없어요더 필요한 게 있나요?:)

그리고 철학적인 질문: 왜 JUnit은 이 프로그램을org.hamcrest오리지널 햄크레스트 라이브러리를 사용하도록 권장하는 대신 자체 배포판으로 패키지화할 수 있습니까?

버전이 1.2 이상인 Hamcrest를 사용하는 경우junit-dep.jar이 항아리에는 Hamcrest 클래스가 없기 때문에 클래스 로딩 문제를 피할 수 있습니다.

JUnit 4.11 이후junit.jar햄크레스트 클래스는 없습니다.할 필요가 없다junit-dep.jar더이상.

junit은 Matchers를 사용하는 assert That()이라는 이름의 새로운 체크어사트 메서드를 제공하며 보다 읽기 쉬운 테스트 코드와 보다 나은 실패 메시지를 제공해야 합니다.

이것을 사용하기 위해서, junit에 몇개의 코어 매처가 포함되어 있습니다.기본 테스트는 이것부터 시작할 수 있습니다.

matchers를 더 많이 사용하고 싶다면 직접 쓰거나 hamcrest lib를 사용할 수 있습니다.

다음 예시는 ArrayList에서 빈 매처를 사용하는 방법을 보여 줍니다.

package com.test;

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

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class EmptyTest {
    @Test
    public void testIsEmpty() {
        List myList = new ArrayList();
        assertThat(myList, is(empty()));

    }
}

(빌드 패스에 hamcrest-all.jar를 포함했습니다)

당신의 질문에 정확히 대답하지는 않았지만, 당신은 FEST-Asert fluent assertions API를 꼭 사용해 보세요.Hamcrest와 경쟁하고 있지만 정적 Import를 하나만 하면 되기 때문에 API가 훨씬 쉽습니다.다음은 FEST를 사용하여 cpater에서 제공하는 코드입니다.

package com.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;

public class EmptyTest {
    @Test
    public void testIsEmpty() {
        List myList = new ArrayList();
        assertThat(myList).isEmpty();
    }  
}

편집: 메이븐 좌표:

<dependency>
  <groupId>org.easytesting</groupId>
  <artifactId>fest-assert</artifactId>
  <version>1.4</version>
  <scope>test</scope>
</dependency>

또, JUnit 4.1.1 + Hamcrest 1.3 + Mockito 1.9.5 를 사용하고 있는 경우는, mockito-all 를 사용하지 말아 주세요.여기에는 Hamcrest 코어 클래스가 포함되어 있습니다.대신 mockito-core를 사용합니다.다음의 설정이 기능합니다.

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>hamcrest-core</artifactId>
            <groupId>org.hamcrest</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.1.1</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>hamcrest-core</artifactId>
            <groupId>org.hamcrest</groupId>
        </exclusion>
    </exclusions>
</dependency>

버전이 항상 변경되고 있기 때문에 2014년 12월 2일 현재 http://www.javacodegeeks.com/2014/03/how-to-test-dependencies-in-a-maven-project-junit-mockito-hamcrest-assertj.html의 설명서가 유효하다는 것을 알리기 위해 투고합니다.AssertJ는 사용하지 않고 다음 항목만 사용합니다.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>   
<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

JUnit은 왜 오리지널 햄크레스트 라이브러리를 사용하도록 권장하지 않고 org.hamcrest 패키지를 자체 배포판에 포함시켰습니까?

일 거야assertThatJUnit은 'JUnit'이라는이죠. 즉 'JUnit'은 'JUnit'이라는 뜻이죠.Assert는 Import를 .org.hamcrest.MatcherJUnit이 Hamcrest에 의존하거나 Hamcrest를 포함(적어도 일부)하지 않는 한 인터페이스를 사용할 수 없습니다.JUnit의 일부를 포함시키는 것이 더 쉬웠기 때문에 의존하지 않고 사용할 수 있었습니다.

2018년 대부분의 최신 라이브러리 사용:

configurations {
    all {
        testCompile.exclude group: "org.hamcrest", module: "hamcrest-core"
        testCompile.exclude group: "org.hamcrest", module: "hamcrest-library"
    }
}
dependencies {
    testCompile("junit:junit:4.12")
    // testCompile("org.hamcrest:hamcrest-library:1.3")
    // testCompile("org.hamcrest:java-hamcrest:2.0.0.0")
    testCompile("org.hamcrest:hamcrest-junit:2.0.0.0")
}

JUnit-4.12와 JUnit-Dep-4.10 모두 각각의 .xml 파일에 따라 Hamcrest 의존관계가 있습니다.

자세한 조사 결과 의존관계는 .xml 파일에서 이루어졌지만 소스와 클래스는 jars에서 발견되었습니다.이는 build.gradle의 의존성을 배제하고 모든 것을 청결하게 유지하기 위해 테스트하는 방법인 것 같습니다.

그냥 F.Y.I.일 뿐이야

언급URL : https://stackoverflow.com/questions/5569394/how-to-use-junit-and-hamcrest-together

반응형