example code for BAEL-2849 (#6701)
* added example code for BAEL-2083 * updated example code for BAEL-2083 * added example code for BAEL-2849
This commit is contained in:
parent
dfc9b0d027
commit
f6aa400ace
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.h2db.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootH2Application {
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplication.run(SpringBootH2Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.h2db.springboot.daos;
|
||||
|
||||
|
||||
|
||||
|
||||
import com.baeldung.h2db.springboot.models.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.h2db.springboot.models;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Table(name = "users")
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
public User() { }
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ spring.datasource.url=jdbc:h2:mem:mydb
|
|||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
debug=true
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung;
|
||||
|
||||
import com.baeldung.h2db.springboot.SpringBootH2Application;
|
||||
import com.baeldung.h2db.springboot.daos.UserRepository;
|
||||
import com.baeldung.h2db.springboot.models.User;
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootH2Application.class)
|
||||
public class SpringBootH2IntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
public void contextLoads() { }
|
||||
|
||||
@Test
|
||||
public void givenUserProfile_whenAddUser_thenCreateNewUser() {
|
||||
User user = new User();
|
||||
user.setFirstName("John");
|
||||
user.setLastName("Doe");
|
||||
userRepository.save(user);
|
||||
List<User> users = (List<User>) userRepository.findAll();
|
||||
assertFalse(users.isEmpty());
|
||||
|
||||
String firstName = "Aliko";
|
||||
String lastName = "Dangote";
|
||||
User user1 = userRepository.findById(users.get(0).getId()).get();
|
||||
user1.setLastName(lastName);
|
||||
user1.setFirstName(firstName);
|
||||
userRepository.save(user1);
|
||||
|
||||
user = userRepository.findById(user.getId()).get();
|
||||
assertEquals(user.getFirstName(), firstName);
|
||||
assertEquals(user.getLastName(), lastName);
|
||||
|
||||
userRepository.deleteById(user.getId());
|
||||
assertTrue( ((List<User>) userRepository.findAll()).isEmpty());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue