Commit 828928f8 authored by vikram singh's avatar vikram singh

converted junit4 to junit 5 added test cases for all the services

parent 75ce3730
......@@ -19,7 +19,8 @@ configurations {
repositories {
flatDir {
dirs 'C:\\Users\\kpadhi\\.m2\\repository\\com\\nisum\\exceptionservice\\0.0.1'
dirs 'C:\\Users\\nmedarametla\\.m2\\repository\\com\\nisum\\exceptionservice\\0.0.1'
// dirs '/Users/nisum/.m2/repository/com/nisum/exceptionservice/0.0.1'
}
mavenCentral()
}
......@@ -34,7 +35,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile 'com.nisum:exceptionservice:0.0.1'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.0'
......@@ -42,20 +46,13 @@ dependencies {
compile 'org.mapstruct:mapstruct:1.3.0.Beta2'
testCompile 'com.squareup.okhttp3:mockwebserver:3.8.0'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.3.RELEASE'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
//testImplementation 'io.projectreactor:reactor-test'
compile 'com.h2database:h2:1.3.156'
testCompile "com.github.tomakehurst:wiremock-jre8:2.26.3"
testCompile group: 'org.springframework.cloud', name: 'spring-cloud-starter-contract-stub-runner', version: '2.2.2.RELEASE'
}
......
#Mon Apr 27 13:41:15 IST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip
......@@ -5,26 +5,33 @@ import com.nisum.offertransactionservice.dto.EndOfSaleReq;
import com.nisum.offertransactionservice.dto.Item;
import com.nisum.offertransactionservice.dto.OfferTransactionResponse;
import com.nisum.offertransactionservice.model.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RunWith(MockitoJUnitRunner.class)
public class EndOfSaleTest {
@Mock
private EndOfSaleService endOfSaleServiceMock;
import static org.junit.jupiter.api.Assertions.*;
public class EndOfSaleTest {
@InjectMocks
private EndOfSaleService endOfSaleService;
......@@ -32,41 +39,55 @@ public class EndOfSaleTest {
@Mock
private EndOfSaleRepo endOfSaleRepo;
@Before
public void setUp() throws Exception {
private static final String expectedUUID="123456788899999998";
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void putEndofSaleResInDbTest() throws IOException {
EndOfSaleReq endOfSaleReq = getEndOfSaleReq();
@DisplayName("This test will be test the putEndofSaleResInDb method ")
@ParameterizedTest
@MethodSource("getEndOfSaleReqSource")
public void putEndofSaleResInDbTest( EndOfSaleReq endOfSaleReq) throws IOException {
Mockito.when(endOfSaleRepo.save(ArgumentMatchers.any())).thenReturn(ArgumentMatchers.any());
String uuid = endOfSaleService.putEndofSaleResInDb(endOfSaleReq);
Assert.assertEquals("123456788899999998",uuid);
assertAll(
"uuid test",
() -> assertEquals(expectedUUID,uuid,()->"uuid should match to the expected"),
() -> assertNotNull(uuid,()->"uuid should not be returned as null")
);
}
@Test
public void putEndofSaleResInDbNegTest() throws IOException {
EndOfSaleReq endOfSaleReq = getEndOfSaleNegReq();
@ParameterizedTest
@MethodSource("getEndOfSaleReqSource")
public void putEndofSaleResInDbNegTest(EndOfSaleReq endOfSaleReq) throws IOException {
Mockito.when(endOfSaleRepo.save(ArgumentMatchers.any())).thenReturn(ArgumentMatchers.any());
String uuid = endOfSaleService.putEndofSaleResInDb(endOfSaleReq);
Assert.assertNotNull(uuid);
assertAll(
"uuid test",
() -> assertNotNull(uuid,()->"uuid should not be returned as null")
);
}
private EndOfSaleReq getEndOfSaleReq() {
private static EndOfSaleReq getEndOfSaleReq() {
EndOfSaleReq endOfSaleReq = new EndOfSaleReq();
endOfSaleReq.setOfferTransactionResponse(retOfferTransRes());
endOfSaleReq.setOffers(returnOffers());
return endOfSaleReq;
}
private EndOfSaleReq getEndOfSaleNegReq() {
EndOfSaleReq endOfSaleReq = new EndOfSaleReq();
endOfSaleReq.setOfferTransactionResponse(retOfferTransNegRes());
endOfSaleReq.setOffers(returnOffers());
return endOfSaleReq;
private static Stream<Arguments> getEndOfSaleReqSource() {
return Stream.of(
Arguments.of(getEndOfSaleReq())
);
}
private OfferTransactionResponse retOfferTransRes() {
private static OfferTransactionResponse retOfferTransRes() {
OfferTransactionResponse offerTransactionResponse = new OfferTransactionResponse();
offerTransactionResponse.setHhid("123");
List<Item> itemList = new ArrayList<>();
......@@ -79,7 +100,7 @@ public class EndOfSaleTest {
return offerTransactionResponse;
}
private OfferTransactionResponse retOfferTransNegRes() {
private static OfferTransactionResponse retOfferTransNegRes() {
OfferTransactionResponse offerTransactionResponse = new OfferTransactionResponse();
offerTransactionResponse.setHhid("123");
List<Item> itemList = new ArrayList<>();
......@@ -92,7 +113,7 @@ public class EndOfSaleTest {
return offerTransactionResponse;
}
public List<OfferLookup> returnOffers() {
public static List<OfferLookup> returnOffers() {
OfferLookup offerLookup1 = new OfferLookup("1", "12", 23L, "02", "AND", "BOGO");
OfferLookup offerLookup2 = new OfferLookup("2", "13", 24L, "02", "OR", "BXGY");
List<OfferLookup> offerLookups = new ArrayList<>();
......
......@@ -2,47 +2,56 @@ package com.nisum.offertransactionservice.service;
import com.nisum.offertransactionservice.client.ClientService;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;
@RunWith(MockitoJUnitRunner.class)
public class EndOfTransactionTest {
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
@Mock
private EndOfTransactionService endOfTransactionServiceMock;
public class EndOfTransactionTest {
@InjectMocks
@Spy
private EndOfTransactionService endOfTransactionService = new EndOfTransactionService();
private EndOfTransactionService endOfTransactionService;
@Mock
ClientService clientService;;
private ClientService clientService;
@Before
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void endOfTransactionTest() {
String uuid = "86fd4146-0540-405b-b621-a95f4ccdfa0d";
Mockito.when(clientService.getStringMono(ArgumentMatchers.any())).thenReturn(Mono.just("test"));
Assert.assertNotNull(endOfTransactionService.endOfTransaction(uuid));
@DisplayName("This test will test the endOfTransactionTest method ")
@ParameterizedTest
@ValueSource(strings={"86fd4146-0540-405b-b621-a95f4ccdfa0d"})
public void endOfTransactionTest(String uuid) {
when(clientService.getStringMono(ArgumentMatchers.any())).thenReturn(Mono.just("test"));
assertNotNull(endOfTransactionService.endOfTransaction(uuid));
}
@Test(expected = GlobalApiGenericException.class)
public void endOfTransactionNegTest() {
String uuid = null;
//Mockito.when(clientService.getStringMono(ArgumentMatchers.any())).thenReturn(Mono.just("test"));
endOfTransactionService.endOfTransaction(uuid);
@DisplayName("This test will test the endOfTransactionTest method ")
@ParameterizedTest
@NullSource
public void endOfTransactionNegTest(String uuid) {
assertThrows(GlobalApiGenericException.class, () -> {
endOfTransactionService.endOfTransaction(uuid);
});
}
}
......@@ -6,39 +6,41 @@ import com.nisum.offertransactionservice.dao.OfferLookupRepo;
import com.nisum.offertransactionservice.dao.OfferMetaDataRepo;
import com.nisum.offertransactionservice.dto.*;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import com.nisum.offertransactionservice.model.*;
import org.assertj.core.util.Lists;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import com.nisum.offertransactionservice.model.OfferLookup;
import com.nisum.offertransactionservice.model.OfferMeta;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
public class OfferServiceTest {
@InjectMocks
private OfferCallingPEService offerCallingPEService;
@Mock
private OfferLookupRepo offerLookupRepo;
@Mock
private OfferMetaDataRepo offerMetaDataRepo;
@InjectMocks
private OfferCallingPEService offerCallingPEService = new OfferCallingPEService();
@Before
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
......@@ -46,22 +48,36 @@ public class OfferServiceTest {
@Mock
ClientService clientService;
@Test
public void serviceTest() {
OfferTransactionRequest offerTransactionRequest = getOfferTransactionRequest();
Mockito.when(offerLookupRepo.findByHhId(ArgumentMatchers.any(),ArgumentMatchers.any())).thenReturn(returnOffers());
Mockito.when(clientService.getPeResponseFlux(ArgumentMatchers.any())).thenReturn(getPeResponse());
Mockito.doReturn(getOfferMeta()).when(offerMetaDataRepo).findByOfferIdAndOfferStatusIgnoreCase(ArgumentMatchers.any(),ArgumentMatchers.any());
@DisplayName("testing the getDiscountedItemList method")
@ParameterizedTest
@MethodSource("getOfferTransactionRequestSource")
public void testGetDiscountedItemList(OfferTransactionRequest offerTransactionRequest) {
when(offerLookupRepo.findByHhId(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(returnOffers());
when(clientService.getPeResponseFlux(ArgumentMatchers.any())).thenReturn(getPeResponse());
doReturn(getOfferMeta()).when(offerMetaDataRepo).findByOfferIdAndOfferStatusIgnoreCase(ArgumentMatchers.any(),ArgumentMatchers.any());
OfferTransactionResponse offerTransactionResponse = offerCallingPEService.getDiscountedItemList(offerTransactionRequest);
Assert.assertNotNull(offerTransactionResponse);
assertNotNull(offerTransactionResponse, ()->"response should not be null");
}
private static Stream<Arguments> getOfferTransactionRequestSource(){
return Stream.of(Arguments.of(getOfferTransactionRequest()));
}
@Test(expected = GlobalApiGenericException.class)
public void serviceNegTest() {
OfferTransactionRequest offerTransactionRequest = getOfferTransactionRequest();
Mockito.when(offerLookupRepo.findByHhId(ArgumentMatchers.any(),ArgumentMatchers.any())).thenReturn(returnOffers());
Mockito.doReturn(Optional.empty()).when(offerMetaDataRepo).findByOfferIdAndOfferStatusIgnoreCase(ArgumentMatchers.any(),ArgumentMatchers.any());
offerCallingPEService.getDiscountedItemList(offerTransactionRequest);
@DisplayName("testing the getDiscountedItemList method")
@ParameterizedTest
@MethodSource("getOfferTransactionRequestSource")
public void getDiscountedItemListNegTest(OfferTransactionRequest offerTransactionRequest) {
when(offerLookupRepo.findByHhId(ArgumentMatchers.any(),ArgumentMatchers.any())).thenReturn(returnOffers());
doReturn(Optional.empty()).when(offerMetaDataRepo).findByOfferIdAndOfferStatusIgnoreCase(ArgumentMatchers.any(),ArgumentMatchers.any());
assertThrows(GlobalApiGenericException.class, () -> {
offerCallingPEService.getDiscountedItemList(offerTransactionRequest);
},
()->"Throwing the GlobalApiGenericException"
);
}
......@@ -101,7 +117,7 @@ public class OfferServiceTest {
return peRequest;
}
private OfferTransactionRequest getOfferTransactionRequest() {
private static OfferTransactionRequest getOfferTransactionRequest() {
OfferTransactionRequest offerTransactionRequest = new OfferTransactionRequest();
offerTransactionRequest.setHhId("123");
List<Item> itemList = new ArrayList<>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment