In this walkthrough, we revisit the Car Integration Tests that we worked on in yesterday’s walkthrough Walkthrough: Spring Integration Tests. We’ll refactor the app to use Postgres and Hibernate.
$ git checkout master
or $ git checkout solution
$ git checkout -b day4-add-hibernate
Each section outlines one task or group of tasks we need to complete.
psql
, create a Postgres user: psql=# create user car_user with encrypted password 'catdogbluehouse';
psql=# create database car;
psql=# grant all privileges on database car to car_user
@Entity
annotationCheck out the Configuration: Spring & Postgres to learn how to configure our Spring app to work with Postgres.
We also will be working with sensitive information, and it would be a good idea to read Configuration: Environment Variables - IntelliJ.
Note
Committing passwords to source control is a BAD idea. We are using a spring data feature that searches for and uses enviroment variables. Read more about Spring Boot/Data configuration.
package org.launchcode.training.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Car {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
//rest of class is not shown...
package org.launchcode.training.data;
import org.launchcode.training.models.Car;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CarRepository extends JpaRepository<Car, Integer> {
}
@Controller
@RequestMapping("car")
public class CarController {
@Autowired
private org.launchcode.training.data.CarRepository carRepository;
@RunWith(SpringRunner.class)
@IntegrationTestConfig
public class CarControllerTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private CarRepository carRepository;
Add a /car-integration-tests/src/test/resources/application-test.properties
file with below contents.
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
We need to make sure our test properaties are used when running tests. Add the below code to /car-integration-tests/src/test/java/org/launchcode/training/IntegrationTestConfig.java
.
The @Transactional
annotation insures that any sql executed during a test will only exist for that single test and won’t pollute another test.
@TestPropertySource(locations = "classpath:application-test.properties")
@Transactional
If not, fix them ;p