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

Junit testcase

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