Skip to content

Instantly share code, notes, and snippets.

View dhaval201279's full-sized avatar
🎯
Focusing

Dhaval Shah dhaval201279

🎯
Focusing
View GitHub Profile
@dhaval201279
dhaval201279 / ReservationEndToEndTest.java
Last active March 11, 2018 01:41
Reservation end to end test
package com.its.reservation;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
@dhaval201279
dhaval201279 / ReservationControllerTest1.java
Last active March 11, 2018 02:27
1st Version of ReservationControllerTest
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.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.its.reservation.web.ReservationController;
@dhaval201279
dhaval201279 / ReservationController1.java
Last active March 11, 2018 02:30
First Version of ReservationController
package com.its.reservation.web;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.its.reservation.repository.Reservation;
/**
@dhaval201279
dhaval201279 / ReservationController2.java
Last active March 11, 2018 02:37
2nd Version of ReservationController
@RestController
@RequestMapping("/reservation")
public class ReservationController {
private ReservationService reservationService;
public ReservationController(ReservationService reservationService) {
this.reservationService = reservationService;
}
@dhaval201279
dhaval201279 / ReservationService1.java
Last active March 11, 2018 02:38
1st version of ReservationService - with null as return type
package com.its.reservation.service;
import org.springframework.stereotype.Service;
import com.its.reservation.repository.Reservation;
@Service
public class ReservationService {
public Reservation getReservationDetails(String name) {
return null;
@dhaval201279
dhaval201279 / ReservationControllerTest2.java
Last active March 11, 2018 02:42
2nd Version of ReservationControllerTest
@RunWith(SpringRunner.class)
@WebMvcTest(ReservationController.class)
public class ReservationControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
ReservationService reservationService;
@Test
@dhaval201279
dhaval201279 / ReservationController3.java
Last active March 11, 2018 02:44
3rd version of ReservationController with Exception Handler
@RestController
@RequestMapping("/reservation")
public class ReservationController {
private ReservationService reservationService;
public ReservationController(ReservationService reservationService) {
this.reservationService = reservationService;
}
@RequestMapping(method = RequestMethod.GET, value = "/{name}")
@dhaval201279
dhaval201279 / ReservationControllerTest3.java
Last active March 11, 2018 02:47
3rd version of ReservationControllerTest
@RunWith(SpringRunner.class)
@WebMvcTest(ReservationController.class)
public class ReservationControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
ReservationService reservationService;
@Test
@dhaval201279
dhaval201279 / ReservationServiceTest1.java
Last active March 11, 2018 02:54
1st version of ReservationServiceTest - which fails initially
package com.its.reservation;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.its.reservation.repository.Reservation;
import com.its.reservation.service.ReservationService;
@dhaval201279
dhaval201279 / ReservationService2.java
Last active March 11, 2018 02:58
2nd version of ReservationService - with required collaborator i.e ReservationRepository
package com.its.reservation.service;
import org.springframework.stereotype.Service;
import com.its.reservation.repository.Reservation;
import com.its.reservation.repository.ReservationRepository;
@Service
public class ReservationService {
private ReservationRepository reservationRepository;