BAEL-2477
This commit is contained in:
parent
4932689c32
commit
602d8b5a09
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.customer;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public Customer(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.customer;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CustomerRepository extends JpaRepository<Customer, Long> {
|
||||
|
||||
List<Customer> findByName(String name);
|
||||
|
||||
List<Customer> findByNameAndEmail(String name, String email);
|
||||
|
||||
@Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)")
|
||||
List<Customer> findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.customer;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
||||
@RunWith(SpringRunner.class)
|
||||
public class CustomerRepositoryIntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Autowired
|
||||
private CustomerRepository repository;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
entityManager.persist(new Customer("A", "A@example.com"));
|
||||
entityManager.persist(new Customer("D", null));
|
||||
entityManager.persist(new Customer("D", "D@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryMethod_whenEmailIsNull_thenFoundByNullEmail() {
|
||||
List<Customer> customers = repository.findByNameAndEmail("D", null);
|
||||
|
||||
assertEquals(1, customers.size());
|
||||
Customer actual = customers.get(0);
|
||||
|
||||
assertEquals(null, actual.getEmail());
|
||||
assertEquals("D", actual.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryMethod_whenEmailIsAbsent_thenIgnoreEmail() {
|
||||
List<Customer> customers = repository.findByName("D");
|
||||
|
||||
assertEquals(2, customers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryAnnotation_whenEmailIsNull_thenIgnoreEmail() {
|
||||
List<Customer> customers = repository.findCustomerByNameAndEmail("D", null);
|
||||
|
||||
assertEquals(2, customers.size());
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue