How to use JUnit and Hamcrest together?

前端 未结 8 988
傲寒
傲寒 2020-12-07 13:03

I can\'t understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers. At the

8条回答
  •  心在旅途
    2020-12-07 13:34

    Not exactly answering your question, but you should definitely try FEST-Assert fluent assertions API. It's competing with Hamcrest, but has a much easier API with only one static import required. Here is the code provided by cpater using FEST:

    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();
        }  
    }
    

    EDIT: Maven coordinates:

    
      org.easytesting
      fest-assert
      1.4
      test
    
    

提交回复
热议问题