Skip to content

Junit 5

Mingtao edited this page Oct 19, 2020 · 8 revisions
<!-- JUnit 5 dependencies -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.4.2</version>
    <scope>test</scope>
</dependency>
<dependency> 
    <groupId>org.junit.platform</groupId> 
    <artifactId>junit-platform-launcher</artifactId> 
    <version>1.4.2</version> 
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency> <!-- Allows the legacy unit tests code under JUnit 4 still to run -->
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>
    
<!-- Mockito dependencies -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.23.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>2.23.4</version>
    <scope>test</scope>
</dependency>
  
<!-- catch exception dependencies -->
<dependency>
    <groupId>eu.codearte.catch-exception</groupId>
    <artifactId>catch-exception</artifactId>
    <version>2.0</version>
    <scope>test</scope>
</dependency>
    
<!-- JMockit used to mock static methods -->
<dependency>
    <groupId>org.jmockit</groupId>
    <artifactId>jmockit</artifactId>
    <version>1.24</version>
    <scope>test</scope>
</dependency>



import static com.googlecode.catchexception.CatchException.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.platform.runner.JUnitPlatform; 
import mockit.MockUp;

@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
class HttpClientUtilTest {

    @Mock private Shoe shoe;
    @Spy private Shoe shoe2 = new Shoe();

    @BeforeEach
    void setUp() throws Exception {
    }

    @Test
    public void hit14StandHard17() {
        Mockito.when(shoe.deal(anyLong(), any(Date.class))).thenReturn(new Card(Rank.FOUR), new Card(Rank.KING), new Card(Rank.THREE)); //This syntax works for Mock
        Mockito.doReturn(new Card(Rank.FOUR), new Card(Rank.KING), new Card(Rank.THREE)).when(shoe2).deal(anyLong(), any(Date.class));  //This syntax works for both Mock and Spy 
        Mockito.verify(job, Mockito.times(0)).listDeletedJobItems();
    }

    @Nested
    @DisplayName("test getRequest()")
    class testGetRequest{

        @BeforeEach
        void setUp() throws Exception {
    
        }

        @Test
            void test() {
        }     
    }
}

/** Mock static method */
new MockUp<HttpClients>() {
    @mockit.Mock
    public CloseableHttpClient createDefault() {
        return httpclient;
    }
};

/** Catch exception */
catchException(() -> HttpClientUtil.postRequest("dummyUri", params, responseHandler)); 
assertThat(caughtException(), instanceOf(IllegalArgumentException.class));

/** Argument captor */
ArgumentCaptor<HttpPost> httpPostArgument = ArgumentCaptor.forClass(HttpPost.class);
verify(httpclient, times(1)).execute(httpPostArgument.capture(), Mockito.eq(responseHandler));
HttpPost httpPost = httpPostArgument.getValue();
assertThat(httpPost.getEntity().getContentLength(), is(0L));

/** Answer */
import org.mockito.ArgumentMatchers;

when(customerEcardConfigService.createCustomerEcardConfig(ArgumentMatchers.isA(BusinessCustomerLink.class)))
  .then((i) -> {
      BusinessCustomerLink link = i.getArgument(0);
      link.setId(1L);
      return null;
});

/** ParameterizedTest */
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@ParameterizedTest
@ValueSource(strings = { "1", Integer.MAX_VALUE+"" })
public void validValue(String value) {
    jobConfig.setName(JobConfigName.BULK_EDIT_THRESHOLD);
    jobConfig.setValue(value);
    assertFalse(jobConfig.validateValue().isPresent());
}
Clone this wiki locally