JAVA-29238 | fixing tests (#15434)

This commit is contained in:
Gaetano Piazzolla 2023-12-18 22:11:10 +01:00 committed by GitHub
parent 94fdc2f4bc
commit 357494e56f
24 changed files with 226 additions and 192 deletions

View File

@ -20,6 +20,6 @@ After importing the project into Eclipse, you may see the following error:
"No persistence xml file found in project" "No persistence xml file found in project"
This can be ignored: This can be ignored:
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project" - Project -> Properties -> Java Persistence -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
Or: Or:
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator - Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator

View File

@ -0,0 +1,21 @@
package com.baeldung.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.jpa.JpaApplication;
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class, basePackages = "com.baeldung.boot.daos")
@EntityScan({"com.baeldung.boot.domain"})
@ComponentScan("com.baeldung.boot.daos")
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}

View File

@ -1,17 +1,17 @@
package com.baeldung; package com.baeldung.jpa;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
@SpringBootApplication @SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class) @ComponentScan("com.baeldung.jpa")
public class Application { @EnableJpaRepositories("com.baeldung.jpa.repository")
public class JpaApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(JpaApplication.class, args);
} }
} }

View File

@ -1,35 +1,35 @@
package com.baeldung.config; package com.baeldung.jpa.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean; import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean; import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.baeldung.entity.Fruit; import com.baeldung.jpa.domain.Fruit;
@Configuration @Configuration
public class JpaPopulators { public class JpaPopulators {
@Bean @Bean
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception { public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean(); Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") }); factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
return factory; return factory;
} }
@Bean @Bean
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() { public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
unmarshaller.setClassesToBeBound(Fruit.class); unmarshaller.setClassesToBeBound(Fruit.class);
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean(); UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
factory.setUnmarshaller(unmarshaller); factory.setUnmarshaller(unmarshaller);
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") }); factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
return factory; return factory;
} }
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.repository; package com.baeldung.jpa.config;
import java.util.Properties; import java.util.Properties;
@ -6,12 +6,10 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -23,24 +21,18 @@ import com.google.common.base.Preconditions;
@Configuration @Configuration
@PropertySource("classpath:persistence.properties") @PropertySource("classpath:persistence.properties")
@ComponentScan("com.baeldung.repository")
//@ImportResource("classpath*:*springDataConfig.xml")
@EnableTransactionManagement @EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.baeldung.repository") //@ImportResource("classpath*:*springDataConfig.xml")
public class PersistenceConfig { public class PersistenceConfig {
@Autowired @Autowired
private Environment env; private Environment env;
public PersistenceConfig() {
super();
}
@Bean @Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource()); em.setDataSource(dataSource());
em.setPackagesToScan("com.baeldung.spring.data.persistence.repository"); em.setPackagesToScan("com.baeldung.jpa.domain");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter); em.setJpaVendorAdapter(vendorAdapter);

View File

@ -1,4 +1,4 @@
package com.baeldung.repository; package com.baeldung.jpa.domain;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,40 +1,40 @@
package com.baeldung.entity; package com.baeldung.jpa.domain;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement @XmlRootElement
@Entity @Entity
public class Fruit { public class Fruit {
@Id @Id
private long id; private long id;
private String name; private String name;
private String color; private String color;
public long getId() { public long getId() {
return id; return id;
} }
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getColor() { public String getColor() {
return color; return color;
} }
public void setColor(String color) { public void setColor(String color) {
this.color = color; this.color = color;
} }
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.entity; package com.baeldung.jpa.domain;
import javax.persistence.Basic; import javax.persistence.Basic;
import javax.persistence.Column; import javax.persistence.Column;

View File

@ -1,4 +1,4 @@
package com.baeldung.entity; package com.baeldung.jpa.domain;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -1,27 +1,27 @@
package com.baeldung.repository; package com.baeldung.jpa.repository;
import java.util.List; import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.baeldung.entity.Fruit; import com.baeldung.jpa.domain.Fruit;
@Repository @Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> { public interface FruitRepository extends JpaRepository<Fruit, Long> {
Long deleteByName(String name); Long deleteByName(String name);
List<Fruit> deleteByColor(String color); List<Fruit> deleteByColor(String color);
Long removeByName(String name); Long removeByName(String name);
List<Fruit> removeByColor(String color); List<Fruit> removeByColor(String color);
@Modifying @Modifying
@Query("delete from Fruit f where f.name=:name or f.color=:color") @Query("delete from Fruit f where f.name=:name or f.color=:color")
int deleteFruits(@Param("name") String name, @Param("color") String color); int deleteFruits(@Param("name") String name, @Param("color") String color);
} }

View File

@ -1,9 +1,10 @@
package com.baeldung.repository; package com.baeldung.jpa.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.baeldung.jpa.domain.Foo;
public interface IFooDAO extends JpaRepository<Foo, Long> { public interface IFooDAO extends JpaRepository<Foo, Long> {

View File

@ -1,13 +1,13 @@
package com.baeldung.repository; package com.baeldung.jpa.repository;
import com.baeldung.entity.Passenger; import com.baeldung.jpa.domain.Passenger;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@Repository @Repository
interface PassengerRepository extends JpaRepository<Passenger, Long> { public interface PassengerRepository extends JpaRepository<Passenger, Long> {
List<Passenger> findByFirstNameIgnoreCase(String firstName); List<Passenger> findByFirstNameIgnoreCase(String firstName);

View File

@ -1,11 +1,11 @@
package com.baeldung.repository; package com.baeldung.jpa.repository;
import java.util.List; import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.baeldung.entity.Song; import com.baeldung.jpa.domain.Song;
@Repository @Repository
public interface SongRepository extends JpaRepository<Song, Long> { public interface SongRepository extends JpaRepository<Song, Long> {

View File

@ -1,10 +1,14 @@
package com.baeldung.repository; package com.baeldung.jpa.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baeldung.jpa.domain.Foo;
import com.baeldung.jpa.repository.IFooDAO;
@Service @Service
public class FooService implements IFooService { public class FooService implements IFooService {
@Autowired @Autowired
private IFooDAO dao; private IFooDAO dao;

View File

@ -0,0 +1,7 @@
package com.baeldung.jpa.service;
import com.baeldung.jpa.domain.Foo;
public interface IFooService {
Foo create(Foo foo);
}

View File

@ -1,5 +0,0 @@
package com.baeldung.repository;
public interface IFooService {
Foo create(Foo foo);
}

View File

@ -1,12 +1,12 @@
[ [
{ {
"_class": "com.baeldung.entity.Fruit", "_class": "com.baeldung.jpa.domain.Fruit",
"name": "apple", "name": "apple",
"color": "red", "color": "red",
"id": 1 "id": 1
}, },
{ {
"_class": "com.baeldung.entity.Fruit", "_class": "com.baeldung.jpa.domain.Fruit",
"name": "guava", "name": "guava",
"color": "green", "color": "green",
"id": 2 "id": 2

View File

@ -7,5 +7,5 @@
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
> >
<jpa:repositories base-package="com.baeldung.repository"/> <jpa:repositories base-package="com.baeldung.jpa.repository"/>
</beans> </beans>

View File

@ -13,11 +13,11 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.Application; import com.baeldung.boot.BootApplication;
import com.baeldung.boot.domain.Student; import com.baeldung.boot.domain.Student;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = {Application.class}) @ContextConfiguration(classes = { BootApplication.class})
@DirtiesContext @DirtiesContext
public class ExtendedStudentRepositoryIntegrationTest { public class ExtendedStudentRepositoryIntegrationTest {
@Resource @Resource

View File

@ -15,11 +15,11 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.baeldung.Application; import com.baeldung.boot.BootApplication;
import com.baeldung.boot.domain.MerchandiseEntity; import com.baeldung.boot.domain.MerchandiseEntity;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = BootApplication.class)
public class InventoryRepositoryIntegrationTest { public class InventoryRepositoryIntegrationTest {
private static final String ORIGINAL_TITLE = "Pair of Pants"; private static final String ORIGINAL_TITLE = "Pair of Pants";

View File

@ -1,4 +1,4 @@
package com.baeldung.repository; package com.baeldung.jpa;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -6,11 +6,16 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.jpa.domain.Foo;
import com.baeldung.jpa.service.IFooService;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = PersistenceConfig.class) @ContextConfiguration(classes = { JpaApplication.class})
@DirtiesContext
public class FooServiceIntegrationTest { public class FooServiceIntegrationTest {
@Autowired @Autowired

View File

@ -1,38 +1,39 @@
package com.baeldung.repository; package com.baeldung.jpa;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.entity.Fruit; import com.baeldung.jpa.domain.Fruit;
import com.baeldung.jpa.repository.FruitRepository;
@RunWith(SpringRunner.class)
@SpringBootTest @RunWith(SpringRunner.class)
public class FruitPopulatorIntegrationTest { @SpringBootTest(classes = JpaApplication.class)
public class FruitPopulatorIntegrationTest {
@Autowired
private FruitRepository fruitRepository; @Autowired
private FruitRepository fruitRepository;
@Test
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() { @Test
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
List<Fruit> fruits = fruitRepository.findAll();
assertEquals("record count is not matching", 2, fruits.size()); List<Fruit> fruits = fruitRepository.findAll();
assertEquals("record count is not matching", 2, fruits.size());
fruits.forEach(fruit -> {
if (1 == fruit.getId()) { fruits.forEach(fruit -> {
assertEquals("apple", fruit.getName()); if (1 == fruit.getId()) {
assertEquals("red", fruit.getColor()); assertEquals("apple", fruit.getName());
} else if (2 == fruit.getId()) { assertEquals("red", fruit.getColor());
assertEquals("guava", fruit.getName()); } else if (2 == fruit.getId()) {
assertEquals("green", fruit.getColor()); assertEquals("guava", fruit.getName());
} assertEquals("green", fruit.getColor());
}); }
} });
} }
}

View File

@ -1,4 +1,4 @@
package com.baeldung.repository; package com.baeldung.jpa;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
@ -8,18 +8,22 @@ import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.entity.Passenger; import com.baeldung.jpa.domain.Passenger;
import com.baeldung.jpa.repository.PassengerRepository;
@DataJpaTest(showSql = false)
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = { JpaApplication.class})
@DirtiesContext
public class PassengerRepositoryIntegrationTest { public class PassengerRepositoryIntegrationTest {
@PersistenceContext @PersistenceContext
@ -28,6 +32,7 @@ public class PassengerRepositoryIntegrationTest {
private PassengerRepository repository; private PassengerRepository repository;
@Before @Before
@Transactional
public void before() { public void before() {
entityManager.persist(Passenger.from("Jill", "Smith")); entityManager.persist(Passenger.from("Jill", "Smith"));
entityManager.persist(Passenger.from("Eve", "Jackson")); entityManager.persist(Passenger.from("Eve", "Jackson"));
@ -36,6 +41,7 @@ public class PassengerRepositoryIntegrationTest {
entityManager.persist(Passenger.from("Siya", "Kolisi")); entityManager.persist(Passenger.from("Siya", "Kolisi"));
} }
@Transactional
@Test @Test
public void givenPassengers_whenMatchingIgnoreCase_thenExpectedReturned() { public void givenPassengers_whenMatchingIgnoreCase_thenExpectedReturned() {
Passenger jill = Passenger.from("Jill", "Smith"); Passenger jill = Passenger.from("Jill", "Smith");

View File

@ -1,4 +1,4 @@
package com.baeldung.repository; package com.baeldung.jpa;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -7,17 +7,19 @@ import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.baeldung.entity.Song; import com.baeldung.jpa.domain.Song;
import com.baeldung.repository.SongRepository; import com.baeldung.jpa.repository.SongRepository;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @ContextConfiguration(classes = { JpaApplication.class })
@Sql(scripts = { "/test-song-data.sql" }) @Sql(scripts = { "/test-song-data.sql" })
@DirtiesContext
public class SongRepositoryIntegrationTest { public class SongRepositoryIntegrationTest {
@Autowired private SongRepository songRepository; @Autowired private SongRepository songRepository;