Spring Boot Configuration Class vs. Properties during Test
I have little experience with the autoconfiguration of Spring Boot. What is the best practice in dealing with the database configuration for the production and the test.
Currently I have a configuration for the production which reads the database properties from a propertie file. What is the difference or what is the configuration for the entity manager. I do not create a bean but only specify the key value pairs in the file.
For the test, however, I have to create the beans, otherwise the message comes:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Currently my test class is as follows:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "de.xxx.xxx", repositoryBaseClass =
ExtendedRepositoryImpl.class)
@EntityScan(basePackages = { "de.xxx.xxx" })
@ComponentScan(basePackages = { "de.xxx.xxx" })
@EnableJpaAuditing
@ActiveProfiles(profiles = { "test" })
public abstract class AbstractTestCase {
protected static final Log LOG = LogFactory.getLog(AbstractTestCase.class);
}
So what is the best in dealing with the configuration. In Java within the @Configuration or per propertie file? When should I use something?
UPDATE 1:
I added the @DataJpaTest annotation and remove the @Bean's from the @Configuration class. It still works, but what is the best practis to deal with? Is there a guide to understand the magic of the annotations?
spring-boot properties spring-data-jpa h2
add a comment |
I have little experience with the autoconfiguration of Spring Boot. What is the best practice in dealing with the database configuration for the production and the test.
Currently I have a configuration for the production which reads the database properties from a propertie file. What is the difference or what is the configuration for the entity manager. I do not create a bean but only specify the key value pairs in the file.
For the test, however, I have to create the beans, otherwise the message comes:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Currently my test class is as follows:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "de.xxx.xxx", repositoryBaseClass =
ExtendedRepositoryImpl.class)
@EntityScan(basePackages = { "de.xxx.xxx" })
@ComponentScan(basePackages = { "de.xxx.xxx" })
@EnableJpaAuditing
@ActiveProfiles(profiles = { "test" })
public abstract class AbstractTestCase {
protected static final Log LOG = LogFactory.getLog(AbstractTestCase.class);
}
So what is the best in dealing with the configuration. In Java within the @Configuration or per propertie file? When should I use something?
UPDATE 1:
I added the @DataJpaTest annotation and remove the @Bean's from the @Configuration class. It still works, but what is the best practis to deal with? Is there a guide to understand the magic of the annotations?
spring-boot properties spring-data-jpa h2
The documentation can help you understand the magic of the annotations docs.spring.io/spring-boot/docs/current/reference/html/… specifically docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 12 '18 at 16:53
Thanks, but i almost read this. My problem is, to unterstand what is the best practice. Property and Annotation or Config class. I also had a problem with combining DataJpaTest and SpringBootTest. The best case is, having a H2TestConfig class and the right configuration. There are many posts on google dealing with different configuration. Now i am a little bit confused.
– Devilluminati
Nov 12 '18 at 23:07
SpringBootTest = full stack, DataJpaTest = only persistence, WebMvcTest = web layer. So it doesn't make sense to combine DataJpaTest with SpringBootTest. One of the points of Spring Boot is that by using the properties, it configures the beans for you. Using a different configuration for test and prod is as easy then as creating separate/resources/application.properties
/test/resources/application.properties
files. See docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 13 '18 at 8:54
Thanks, this is what i am looking for
– Devilluminati
Nov 13 '18 at 9:18
add a comment |
I have little experience with the autoconfiguration of Spring Boot. What is the best practice in dealing with the database configuration for the production and the test.
Currently I have a configuration for the production which reads the database properties from a propertie file. What is the difference or what is the configuration for the entity manager. I do not create a bean but only specify the key value pairs in the file.
For the test, however, I have to create the beans, otherwise the message comes:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Currently my test class is as follows:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "de.xxx.xxx", repositoryBaseClass =
ExtendedRepositoryImpl.class)
@EntityScan(basePackages = { "de.xxx.xxx" })
@ComponentScan(basePackages = { "de.xxx.xxx" })
@EnableJpaAuditing
@ActiveProfiles(profiles = { "test" })
public abstract class AbstractTestCase {
protected static final Log LOG = LogFactory.getLog(AbstractTestCase.class);
}
So what is the best in dealing with the configuration. In Java within the @Configuration or per propertie file? When should I use something?
UPDATE 1:
I added the @DataJpaTest annotation and remove the @Bean's from the @Configuration class. It still works, but what is the best practis to deal with? Is there a guide to understand the magic of the annotations?
spring-boot properties spring-data-jpa h2
I have little experience with the autoconfiguration of Spring Boot. What is the best practice in dealing with the database configuration for the production and the test.
Currently I have a configuration for the production which reads the database properties from a propertie file. What is the difference or what is the configuration for the entity manager. I do not create a bean but only specify the key value pairs in the file.
For the test, however, I have to create the beans, otherwise the message comes:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Currently my test class is as follows:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "de.xxx.xxx", repositoryBaseClass =
ExtendedRepositoryImpl.class)
@EntityScan(basePackages = { "de.xxx.xxx" })
@ComponentScan(basePackages = { "de.xxx.xxx" })
@EnableJpaAuditing
@ActiveProfiles(profiles = { "test" })
public abstract class AbstractTestCase {
protected static final Log LOG = LogFactory.getLog(AbstractTestCase.class);
}
So what is the best in dealing with the configuration. In Java within the @Configuration or per propertie file? When should I use something?
UPDATE 1:
I added the @DataJpaTest annotation and remove the @Bean's from the @Configuration class. It still works, but what is the best practis to deal with? Is there a guide to understand the magic of the annotations?
spring-boot properties spring-data-jpa h2
spring-boot properties spring-data-jpa h2
edited Nov 12 '18 at 15:51
asked Nov 12 '18 at 15:06
Devilluminati
86
86
The documentation can help you understand the magic of the annotations docs.spring.io/spring-boot/docs/current/reference/html/… specifically docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 12 '18 at 16:53
Thanks, but i almost read this. My problem is, to unterstand what is the best practice. Property and Annotation or Config class. I also had a problem with combining DataJpaTest and SpringBootTest. The best case is, having a H2TestConfig class and the right configuration. There are many posts on google dealing with different configuration. Now i am a little bit confused.
– Devilluminati
Nov 12 '18 at 23:07
SpringBootTest = full stack, DataJpaTest = only persistence, WebMvcTest = web layer. So it doesn't make sense to combine DataJpaTest with SpringBootTest. One of the points of Spring Boot is that by using the properties, it configures the beans for you. Using a different configuration for test and prod is as easy then as creating separate/resources/application.properties
/test/resources/application.properties
files. See docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 13 '18 at 8:54
Thanks, this is what i am looking for
– Devilluminati
Nov 13 '18 at 9:18
add a comment |
The documentation can help you understand the magic of the annotations docs.spring.io/spring-boot/docs/current/reference/html/… specifically docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 12 '18 at 16:53
Thanks, but i almost read this. My problem is, to unterstand what is the best practice. Property and Annotation or Config class. I also had a problem with combining DataJpaTest and SpringBootTest. The best case is, having a H2TestConfig class and the right configuration. There are many posts on google dealing with different configuration. Now i am a little bit confused.
– Devilluminati
Nov 12 '18 at 23:07
SpringBootTest = full stack, DataJpaTest = only persistence, WebMvcTest = web layer. So it doesn't make sense to combine DataJpaTest with SpringBootTest. One of the points of Spring Boot is that by using the properties, it configures the beans for you. Using a different configuration for test and prod is as easy then as creating separate/resources/application.properties
/test/resources/application.properties
files. See docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 13 '18 at 8:54
Thanks, this is what i am looking for
– Devilluminati
Nov 13 '18 at 9:18
The documentation can help you understand the magic of the annotations docs.spring.io/spring-boot/docs/current/reference/html/… specifically docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 12 '18 at 16:53
The documentation can help you understand the magic of the annotations docs.spring.io/spring-boot/docs/current/reference/html/… specifically docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 12 '18 at 16:53
Thanks, but i almost read this. My problem is, to unterstand what is the best practice. Property and Annotation or Config class. I also had a problem with combining DataJpaTest and SpringBootTest. The best case is, having a H2TestConfig class and the right configuration. There are many posts on google dealing with different configuration. Now i am a little bit confused.
– Devilluminati
Nov 12 '18 at 23:07
Thanks, but i almost read this. My problem is, to unterstand what is the best practice. Property and Annotation or Config class. I also had a problem with combining DataJpaTest and SpringBootTest. The best case is, having a H2TestConfig class and the right configuration. There are many posts on google dealing with different configuration. Now i am a little bit confused.
– Devilluminati
Nov 12 '18 at 23:07
SpringBootTest = full stack, DataJpaTest = only persistence, WebMvcTest = web layer. So it doesn't make sense to combine DataJpaTest with SpringBootTest. One of the points of Spring Boot is that by using the properties, it configures the beans for you. Using a different configuration for test and prod is as easy then as creating separate
/resources/application.properties
/test/resources/application.properties
files. See docs.spring.io/spring-boot/docs/current/reference/html/…– AlexB
Nov 13 '18 at 8:54
SpringBootTest = full stack, DataJpaTest = only persistence, WebMvcTest = web layer. So it doesn't make sense to combine DataJpaTest with SpringBootTest. One of the points of Spring Boot is that by using the properties, it configures the beans for you. Using a different configuration for test and prod is as easy then as creating separate
/resources/application.properties
/test/resources/application.properties
files. See docs.spring.io/spring-boot/docs/current/reference/html/…– AlexB
Nov 13 '18 at 8:54
Thanks, this is what i am looking for
– Devilluminati
Nov 13 '18 at 9:18
Thanks, this is what i am looking for
– Devilluminati
Nov 13 '18 at 9:18
add a comment |
1 Answer
1
active
oldest
votes
Best practice is to let Spring Boot configure beans using application properties in a application.properties
file https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties.
You can use separate configuration by using separate application.properties
files in resources/application.properties
and test/resources/application.properties
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264944%2fspring-boot-configuration-class-vs-properties-during-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Best practice is to let Spring Boot configure beans using application properties in a application.properties
file https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties.
You can use separate configuration by using separate application.properties
files in resources/application.properties
and test/resources/application.properties
.
add a comment |
Best practice is to let Spring Boot configure beans using application properties in a application.properties
file https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties.
You can use separate configuration by using separate application.properties
files in resources/application.properties
and test/resources/application.properties
.
add a comment |
Best practice is to let Spring Boot configure beans using application properties in a application.properties
file https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties.
You can use separate configuration by using separate application.properties
files in resources/application.properties
and test/resources/application.properties
.
Best practice is to let Spring Boot configure beans using application properties in a application.properties
file https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties.
You can use separate configuration by using separate application.properties
files in resources/application.properties
and test/resources/application.properties
.
answered Nov 13 '18 at 9:01
AlexB
1043
1043
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264944%2fspring-boot-configuration-class-vs-properties-during-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The documentation can help you understand the magic of the annotations docs.spring.io/spring-boot/docs/current/reference/html/… specifically docs.spring.io/spring-boot/docs/current/reference/html/…
– AlexB
Nov 12 '18 at 16:53
Thanks, but i almost read this. My problem is, to unterstand what is the best practice. Property and Annotation or Config class. I also had a problem with combining DataJpaTest and SpringBootTest. The best case is, having a H2TestConfig class and the right configuration. There are many posts on google dealing with different configuration. Now i am a little bit confused.
– Devilluminati
Nov 12 '18 at 23:07
SpringBootTest = full stack, DataJpaTest = only persistence, WebMvcTest = web layer. So it doesn't make sense to combine DataJpaTest with SpringBootTest. One of the points of Spring Boot is that by using the properties, it configures the beans for you. Using a different configuration for test and prod is as easy then as creating separate
/resources/application.properties
/test/resources/application.properties
files. See docs.spring.io/spring-boot/docs/current/reference/html/…– AlexB
Nov 13 '18 at 8:54
Thanks, this is what i am looking for
– Devilluminati
Nov 13 '18 at 9:18