I have ,probably a basic question,related to unit testing a spring boot application that uses RestControllers,JPA repositories.Oracle is used in the backend.Now to unit test the JPA repositories using junit,mockito and probably DataJPA,do the tables that are present in the backend,need to be recreated in the H2 in-memory db?
Related
In my spring boot application, there is a persistent context class for data source connectivity. While I write junit for this application, it always call this configuration class and fails when database connection is off. The junit didn't run if I exclude this configuration class. What is the solution to run the junit,even the database connectivity fails.
I need to integrate my application with Javars. But, my application is multi tenant.
I am trying to integrate JaVers mongodb. I succeded in integrating JaVers. But, when the app is started, JaVers storing it's collections jv_snapshots and jv_head_id in the test database. If I delete the test db and again start the application, still the test db is automatically created and the test db would use by the JaVers.
So, How can I tell JaVers to use my own DB in a multi tenant architecture?
If you are not using Javers' Spring Boot starter:
def mongoRepository = new MongoRepository(MongoClients.create().getDatabase("my_database"))
javers = javers().registerJaversRepository(mongoRepository).build()
If you are using Javers' Spring Boot starter, Javers will automatically connect to your application's database.
And there is no notion of tenant in Javers. All you can do is to create one Javers instance per tenant or more generally one Javers instance per one db configuration.
There is spring boot application with h2 database which is used as primary database. Also there is a resource/schema.sql wich is loaded at startup by spring boot.
But during integration tests with #SpringBootTest spring boot does not load this schema.sql. Instead it requires to setup embeded database while there is h2 db already.
Is there a way to execute schema.sql without embeded datasource configuration? And do it only once for all tests (e.g. using #Sql for schema creation for all test is not a solution)?
Annotate your class or method with #Sql(scripts = "classpath:schema.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) where your schema.sql is on resources folder.
You need this if your tests are marked as integration tests. Spring boot will do this for you if you are running a unit test or just compiling with unit tests enabled which is the default.
Set this in your properties file and then rename schema.sql to schema-test.sql
spring.datasource.platform=test
Spring boot automatically configures the embedded database for you as long as you have the it in the classpath (h2, hsqldb or derby)
Currently I have project with configured data sources for two databases, it works fine.
But after adding spring data rest module it does not work, I am getting such error:
IllegalArgumentException: Not an managed type: class....
It appears only after adding #RepositoryRestResource annotation. Seems like Spring Data Rest is not synchronized with jpa repositories configuration, does anybody have example of working spring data rest jpa repositories with two databases?
What I want to do is to create a full Spring WebMVC CRUD API from database tables/Hibernate JPA entities, into an existing Maven Web Project.
What I want generate, to be precise:
Spring WebMVC controller (mapping&implementation)
The JPA entity (if not given) - ofc. using the standard generation built in to most IDEs
Spring Data JPA repository
Optionally modifying some other files (f.e. adding the entity to existing persistence.xml)
Is there a project for this?
What would be the best way to create something like this?
What I had thought about:
Standalone Java APP
Maven subgenerators
Eclipse plugin
Standard code generation methods (f.e. Acceleo plugin)
What would you suggest?
Partial Answer :
If you can generate JPA entities and Spring Data JPA Repositories, you can use Spring Data REST to expose the repositories as a full CRUD REST API.
Although Spring Roo will generate a CRUD application for you, however the code it generates is outdated. If I where starting a new project, I would definitely stay away from it (at least until a new version is released).
I suggest you take a look at JHipster which is a Yeoman that will generate a CRUD application using some of pretty hot tech (like Spring Boot, AngularJS etc.)
Besides the novelty factor of the generated code, another great feature of the project is that it is extremely active as is evident from the releases
The response is probably in this tutorial :
https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa
You can take a look at crud-rest-gen project which explains how to use the crud-maven-plugin to generate :
the CRUD Rest API
the documentation of the API
the HTML5/AngularJS CRUD Administration UI
the Rest API for retrieving audit information and associated unit tests if you use Hibernate Envers to audit your entities
All you have to provide is the data model containing the JPA entities.
Spring-roo (link) have these features, and if you want you can remove spring-roo from your project and remains a java-spring project.
Sample from link :
roo> hint
roo> project --topLevelPackage com.foo
roo> jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
roo> entity jpa --class ~.Timer --testAutomatically
roo> field string --fieldName message --notNull
roo> hint web mvc
roo> web mvc setup
roo> web mvc all --package ~.web
roo> selenium test --controller ~.web.TimerController
roo> web gwt setup
roo> web gwt all --proxyPackage ~.client.proxy --requestPackage ~.client.request
roo> perform tests
roo> quit
As you can see : create the project, setup jpa provider, create Entity, create MVC Controller, the Tests and some GWT setup.
A Yeoman generator for generating Microservices with SpringBoot in Hexagonal Architecture
https://www.npmjs.com/package/generator-springboot-hexagonal