Commit bb1c1495 authored by sharma072072's avatar sharma072072

controller test case added

parent c68a9652
Pipeline #2292 failed with stages
in 13 seconds
......@@ -19,4 +19,8 @@ public class StocksService {
public List<Stocks> getStocks() {
return repository.findAll();
}
public List<Stocks> saveStocks(Iterable<Stocks> stocksIterable) {
return repository.saveAll(stocksIterable);
}
}
package com.altimetrik.ee.demo.tests;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import com.altimetrik.ee.demo.bean.Stocks;
import com.altimetrik.ee.demo.controller.StocksController;
import com.altimetrik.ee.demo.service.StocksService;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@RunWith(SpringRunner.class)
@WebMvcTest({StocksController.class})
public class StocksControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
StocksService stocksService;
@Test
public void getStocksByName() throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
List<Stocks> stocksList = Arrays.asList(
new Stocks("HCL", "HCL Tech", date1, 420.0, "INR"),
new Stocks("IBM", "IBM Tech", date1, 520.0, "USD"),
new Stocks("UPY", "UPY Tech", date1, 520.0, "JPY")
);
when(stocksService.getStocksByIds(any())).thenReturn(stocksList);
mockMvc.perform(
MockMvcRequestBuilders.get("/stocks/stockQuote?symbols=HCL,IBM"))
.andExpect(status().isOk())
.andExpect(content().json(JSON_STRING));
}
String JSON_STRING = "[{\"stockQuote\":\"HCL\",\"companyName\":\"HCL Tech\","
+ "\"date\":\"1998-12-30T18:30:00.000+0000\",\"price\":420.0,\"currency\":\"INR\"},"
+ "{\"stockQuote\":\"IBM\",\"companyName\":\"IBM Tech\",\"date\":\"1998-12-30T18:30:00.000+0000\","
+ "\"price\":520.0,\"currency\":\"USD\"},{\"stockQuote\":\"UPY\",\"companyName\":\"UPY Tech\","
+ "\"date\":\"1998-12-30T18:30:00.000+0000\",\"price\":520.0,\"currency\":\"JPY\"}]";
@Test
public void WhenNoParameterIsPassed() throws Exception {
when(stocksService.getStocksByIds(any())).thenReturn(Collections.emptyList());
mockMvc.perform(
MockMvcRequestBuilders.get("/stocks/stockQuote?symbols="))
.andExpect(status().isNoContent());
}
}
......@@ -18,7 +18,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
public class StocksJPATest {
public class StocksRepositoryTest {
@Autowired
private StocksRepository stocksRepository;
......
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