Commit 4ea20446 authored by Naren Medarametla's avatar Naren Medarametla

Junit testcase

parent 96a10913
...@@ -2,9 +2,12 @@ package com.nisum.offertransactionservice.config; ...@@ -2,9 +2,12 @@ package com.nisum.offertransactionservice.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.client.*; import org.springframework.web.client.*;
@Configuration @Configuration
@PropertySource("classpath:application.properties")
public class OfferTransactionConfig { public class OfferTransactionConfig {
@Bean @Bean
......
...@@ -6,21 +6,24 @@ import com.nisum.offertransactionservice.model.PERequest; ...@@ -6,21 +6,24 @@ import com.nisum.offertransactionservice.model.PERequest;
import com.nisum.offertransactionservice.model.PEResponse; import com.nisum.offertransactionservice.model.PEResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.web.client.*; import org.springframework.web.client.*;
@Component @Component
@PropertySource("classpath:application.properties")
public class OfferDao { public class OfferDao {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Value("${promotion.engine.calculate.discount.url}") /*Value("${promotion.engine.calculate.discount.url}")
private String promotionEngineUrl ; private String promotionEngineUrl ;*/
public PEResponse callPE(PERequest peRequest) { public PEResponse callPE(PERequest peRequest) {
ResponseEntity<PEResponse> peResponse = restTemplate.postForEntity("http://localhost:8081"+promotionEngineUrl,peRequest,PEResponse.class); ResponseEntity<PEResponse> peResponse = restTemplate.postForEntity("http://localhost:8081/promotionEngine/calculateDiscount",peRequest,PEResponse.class);
return peResponse.getBody(); return peResponse.getBody();
} }
} }
package com.nisum.offertransactionservice.dao; package com.nisum.offertransactionservice.dao;
import com.nisum.offertransactionservice.config.OfferTransactionConfig;
import com.nisum.offertransactionservice.model.Item; import com.nisum.offertransactionservice.model.Item;
import com.nisum.offertransactionservice.model.OfferLookup; import com.nisum.offertransactionservice.model.OfferLookup;
import com.nisum.offertransactionservice.model.PERequest; import com.nisum.offertransactionservice.model.PERequest;
import com.nisum.offertransactionservice.model.PEResponse; import com.nisum.offertransactionservice.model.PEResponse;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
...@@ -20,24 +28,32 @@ import org.springframework.web.client.RestTemplate; ...@@ -20,24 +28,32 @@ import org.springframework.web.client.RestTemplate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@RunWith(MockitoJUnitRunner.class)
@PropertySource("classpath:application.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {OfferDao.class})
public class OfferDaoTest { public class OfferDaoTest {
@Autowired
@Mock
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Autowired @InjectMocks
private OfferDao offerDao; private OfferDao offerDao = new OfferDao();
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
/*@Value("${promotion.engine.calculate.discount.url}") /*@Value("${promotion.engine.calculate.discount.url}")
private String promotionEngineUrl ;*/ private String promotionEngineUrl ;*/
@Test @Test
public void test(){ public void test(){
PEResponse peResponse = offerDao.callPE(getPeRequest());
Mockito.when(restTemplate.postForEntity("http://localhost:8081/promotionEngine/calculateDiscount",getPeRequest(), PEResponse.class)) Mockito.when(restTemplate.postForEntity("http://localhost:8081/promotionEngine/calculateDiscount",getPeRequest(), PEResponse.class))
.thenReturn(new ResponseEntity<PEResponse>(getPeResponse(), HttpStatus.OK)); .thenReturn(new ResponseEntity<PEResponse>(getPeResponse(), HttpStatus.OK));
PEResponse peResponse = offerDao.callPE(getPeRequest());
Assert.assertEquals(peResponse,getPeResponse()); Assert.assertEquals(peResponse,getPeResponse());
} }
......
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