Skip to content

Commit

Permalink
Upgrade to 2.0.5 and Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored and Putta Karthik Amarnath committed Sep 13, 2018
1 parent a971ac0 commit 266f364
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 48 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.demo</groupId>
<artifactId>tdd</artifactId>
<artifactId>springboot-tdd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.11.RELEASE</version>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/demo/tdd/controller/CarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.demo.tdd.domain.Car;
import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.service.CarService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class CarController {

Expand All @@ -25,6 +29,7 @@ private Car getCart(@PathVariable("name") String carName){
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
private void carNotFoundHandler(CarNotFoundException ex){
log.error("Entering and leaving CarController : carNotFoundHandler");
}


Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/demo/tdd/domain/Car.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package com.demo.tdd.domain;

import lombok.Data;
import lombok.Generated;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import lombok.Data;

@Data
@Entity
@NoArgsConstructor
@ToString
public class Car {

@Id
@Id
@GeneratedValue
private Long id;

private String name;

private String type;

public Car(String name, String type) {
this.name = name;
this.type = type;
}
this.name = name;
this.type = type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

public class CarNotFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;

}
4 changes: 2 additions & 2 deletions src/main/java/com/demo/tdd/repository/CarRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.demo.tdd.repository;

import com.demo.tdd.domain.Car;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RestController;

import com.demo.tdd.domain.Car;

@Repository
public interface CarRepository extends JpaRepository<Car,Long> {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/demo/tdd/service/CarService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.demo.tdd.service;

import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.domain.Car;
import com.demo.tdd.repository.CarRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Slf4j
import com.demo.tdd.domain.Car;
import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.repository.CarRepository;

@Service
public class CarService {

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/import.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INSERT INTO car (id,name,type) VALUES (1,'prius','hybrid');
INSERT INTO car (id,name,type) VALUES (100,'prius','hybrid');
16 changes: 8 additions & 8 deletions src/test/java/com/demo/tdd/CachingTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.demo.tdd;

import com.demo.tdd.config.CachingConfig;
import com.demo.tdd.domain.Car;
import com.demo.tdd.repository.CarRepository;
import com.demo.tdd.service.CarService;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,10 +14,9 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import com.demo.tdd.domain.Car;
import com.demo.tdd.repository.CarRepository;
import com.demo.tdd.service.CarService;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/com/demo/tdd/controller/CarControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.demo.tdd.controller;

import com.demo.tdd.domain.Car;
import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.service.CarService;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,10 +14,9 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.demo.tdd.domain.Car;
import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.service.CarService;

@RunWith(SpringRunner.class)
@WebMvcTest(CarController.class)
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/com/demo/tdd/repository/CarRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.demo.tdd.repository;

import com.demo.tdd.domain.Car;
import org.assertj.core.api.Assertions;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;
import com.demo.tdd.domain.Car;


@RunWith(SpringRunner.class)
Expand All @@ -28,10 +28,11 @@ public void findByName() {
// This will get the test entity manager to insert the object to the database
// and get recreate it.
// if were to use jpa save method, then you are just testing on the cache but not the actual persistence
// Car savedCar = entityManager.persistFlushFind(new Car("prius","hybrid"));
Car savedCar = entityManager.persistFlushFind(new Car("prius2","hybrid"));
assertThat(savedCar.getId()).isNotNull().isNotNegative();
Car car = repository.findByName("prius");

Assertions.assertThat(car.getName()).isEqualTo("prius");
assertThat(car.getName()).isEqualTo("prius");

}
}
13 changes: 7 additions & 6 deletions src/test/java/com/demo/tdd/service/CarServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.demo.tdd.service;

import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.domain.Car;
import com.demo.tdd.repository.CarRepository;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import com.demo.tdd.domain.Car;
import com.demo.tdd.exception.CarNotFoundException;
import com.demo.tdd.repository.CarRepository;

@RunWith(MockitoJUnitRunner.class)
public class CarServiceTest {
Expand Down

0 comments on commit 266f364

Please sign in to comment.