* BAEL-2799

* fix import lombok

* move to proper module

* filename fix

* filename fix

* fix tests

* fix test methods

* fix test methods
This commit is contained in:
Krzysztof Majewski 2019-05-04 06:33:45 +02:00 committed by maibin
parent 0f6594a1e0
commit ccd28428f3
4 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.jpadefaultvalues.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.jpadefaultvalues.application;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
@Column(columnDefinition = "varchar(255) default 'John Snow'")
private String name = "John Snow";
@Column(columnDefinition = "integer default 25")
private Integer age = 25;
@Column(columnDefinition = "boolean default false")
private Boolean locked = false;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getLocked() {
return locked;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.jpadefaultvalues.application;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

View File

@ -0,0 +1,56 @@
package com.baeldung.jpadefaultvalues.application;
import com.baeldung.jpadefaultvalues.application.User;
import com.baeldung.jpadefaultvalues.application.UserRepository;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDefaultValuesUnitTest {
@Autowired
private UserRepository userRepository;
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithDefaultFieldValues() {
User user = new User();
user = userRepository.save(user);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithNullName() {
User user = new User();
user.setName(null);
user = userRepository.save(user);
assertNull(user.getName());
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@Test
public void saveUser_shouldSaveWithDefaultSqlValues() {
User user = new User();
user = userRepository.save(user);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
}