Sunday, 12 May 2019

JUnit 5 / Mockito / Catch-exception/ Jmockit pom and sample code

pom
<!-- 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> <!-- 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>

Test case template


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

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

    @Mock CloseableHttpClient httpclient;
    
    @BeforeEach
    void setUp() throws Exception {
        
    }

    @Test
    void test(){

    }

    @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));

Sample test cases

https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing-nested-tests/
https://www.petrikainulainen.net/programming/testing/writing-clean-tests-to-verify-or-not-to-verify/
https://www.baeldung.com/jmockit-static-method

No comments:

Post a Comment