Skip to content

Commit

Permalink
mockito base
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitdas13 committed Feb 2, 2022
1 parent 2124df8 commit 10785a7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@
# Maven
log/
target/

#Test
src/test/
62 changes: 51 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.razorpay</groupId>
Expand Down Expand Up @@ -33,15 +33,55 @@
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>


<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
<scope>test</scope>
</dependency>


<!-- test dependencies -->


<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
Expand Down Expand Up @@ -71,7 +111,7 @@
<artifactId>commons-text</artifactId>
<version>1.3</version>
</dependency>

</dependencies>

<distributionManagement>
Expand All @@ -86,14 +126,14 @@
</distributionManagement>

<build>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
Expand Down Expand Up @@ -147,7 +187,7 @@
</executions>
</plugin>
</plugins>

</build>
</project>

</project>
4 changes: 4 additions & 0 deletions src/test/java/com/razorpay/AddonClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import static org.junit.Assert.*;
public class AddonClientTest {

}
63 changes: 63 additions & 0 deletions src/test/java/com/razorpay/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.razorpay;

import okhttp3.*;
import org.json.JSONObject;
import org.junit.Before;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.OngoingStubbing;
import org.springframework.util.ReflectionUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BaseTest {
@InjectMocks
protected AddonClient client = new AddonClient("test");
private OkHttpClient okHttpClient;
Response mockedResponse;

@Before
public void setUp() throws Exception {

MockitoAnnotations.initMocks(this);
mockGetCall();

}

private void mockGetCall() throws IOException {

okHttpClient = mock(OkHttpClient.class);
mockedResponse = mock(Response.class);
Field clientField = ReflectionUtils.findField(ApiUtils.class, "client", OkHttpClient.class);
clientField.setAccessible(true);
ReflectionUtils.setField(clientField,new ApiUtils(),okHttpClient);
Call call = mock(Call.class);
when(call.execute()).thenReturn(mockedResponse);
when(okHttpClient.newCall(anyObject())).thenReturn(call);
}

protected void mockResponseHTTPCodeFromExternalClient(int code)
{
when(mockedResponse.code()).thenReturn(200);
}
protected void mockURL(List<String> urlString)
{
HttpUrl url = mock(HttpUrl.class);
when(url.pathSegments()).thenReturn(urlString);
Request request = mock(Request.class);
when(request.url()).thenReturn(url);
when(mockedResponse.request()).thenReturn(request);
}
protected void mockResponseFromExternalClient(String jsonObject) throws IOException {
JSONObject parse = new JSONObject(jsonObject);
ResponseBody rb = mock(ResponseBody.class);
when(rb.string()).thenReturn(parse.toString());
when(mockedResponse.body()).thenReturn(rb);
}
}

0 comments on commit 10785a7

Please sign in to comment.