In my webapplication there are two datasource beans in the applicationContext one is to be used for real-env and one is to be used for execution of test. The dataSource objects is injected in one DAO class. Using Spring profile how can I configure test dataSource should be injected in the DAO while running Test (JUnit) and real-env dataSource should be injected in the DAO while code deployed to real-env ?
Injecting specific bean to DAO using Spring profile
1k Views Asked by SyParth At
1
There are 1 best solutions below
Related Questions in SPRING
- HTTPS configuration in Spring Boot, server returning timeout
- Multi Tenancy in Spring - Partitioned Data Approach
- How to create beans of the same class for multiple template parameters in Spring
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Springboot: How to get an entity optional property and check null?
- How do I propagate the current SecurityContext to my @RabbitListener in Spring Boot?
- Spring's XML based bean configuration for Object Mapper's Case Insensitive property
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. I'm using Postgresql
- springboot class org.hibernate.mapping.Bag cannot be cast to class org.hibernate.mapping.SimpleValue
- Issue while deploying JDK 17 and Spring 6 application in Tomcat 10.1.20
- Spring JPA Data Auditing - How to design it?
- Springframework test: Async not started
- Error: Cannot invoke "jakarta.servlet.http.HttpSession.getAttribute(String)" because "session" is null
- How does spring-retry determine which methods to retry when @Retryable is placed at the class level?
- problem with edge server registration in Eureka
Related Questions in DEPENDENCY-INJECTION
- How to add logging to an abstract class in php
- Nest.js can't resolve dependencies of the external library's Reflector dependency
- Do we need IoC containers in typescript if ts-mock-imports exists
- Blazor/Razor resolve components using dependency injection
- Access Registed Scoped Services and Transient Services using GetService()
- using state data class alongside ComposeDestinations
- Messing up with conflict between spring jcl and commons-logging.jar
- How to write pytest tests for a FastAPI route involving dependency injection with Pydantic models using Annotated and Depends?
- .NET Core Include Method in Dependency Injection
- Injecting IHubContext into my background service for SignalR in .NET 5
- Issue with service method call in .NET Background Service , Issue with Scope
- How can I inject the prisma io module using inversify in my node.js project?
- Angular service injection hierarchy
- Trouble with "dotnet run seeddata" when trying to seed a database with EntityFrameworkCore. Specifically with ASP.NET Core 6 Web API
- NX Angular unit tests fail because of NullInjectorError
Related Questions in JUNIT4
- Execute JUnit suite with Maven Fork in Customized runOrder
- Unit Testing StateFlow Created By StateIn
- Getting NestedServletException while mocking Resttemplate object in Spring Boot
- How to test a Spring method for adding a file to GridFS with JUnit and Mockito?
- trying to run in parallel for cucumber junit not working
- Junit with Mockito for java
- Testing ProtoDatastore with JUnit4
- Junit4 : Misplaced or misused argument matcher detected here
- restDocs requestParameters cannot find
- Run instrumented test inside module's androidTest
- @MockBean does not get mocked when using @DataJpaTest
- Unit test case in Springboot MockMvc returns 403 Forbidden
- cannot access org.junit.rules.TestRule withUser("testcontainers") (an integration test for ActiveMQ Artemis)
- Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.junit.internal.runners.ErrorReportingRunner
- Maven Surefire Plugin Error: Tests in Error - initializationError
Related Questions in SPRING-PROFILES
- @Value is not taking value from specified @PropertySource when two properties files have same key
- Switching between Spring Profiles
- Spring Boot 3.2.1 multiple .properties files per profile
- Set profile based on the REST API request spring boot
- How to override name of parent properties file?
- Best practice for dev-only Spring beans
- Profile specific application-{profile}.yaml file is not loading in spring boot 2.4.1
- Spring Boot property resolution fails tests at build time when adding a profile
- Spring Cloud Config Server and Client with multiple profiles
- How to assign different value to a spring bean variable from yml file based on spring profile
- SpringBoot: using one yml in another yml
- How to load initial data using H2 and Flyway in Spring Boot
- is there any other way to activate the profile and use the corrosponding application-{phase}.yml?
- How to set up profile in spring boot to mock external api for development
- getting the error in eclipse specifying spring profiles in single yml file
Related Questions in SPRINGJUNIT4CLASSRUNNER
- unable to mock RestTemplate response
- JmsListenerContainerFacotory Not available: NoSuchBeanDefinitionException, even though have configured a DefaultJmsListenerContainerFactory
- maven does not detect tests from the command line
- SpringJunit4Runner no tests found
- How to write junit for RabbitListenerEndpointRegistry in spring framework
- I have an apache camel test case which is failing because of different object instances getting created. The object instances are having different IDs
- Migrating Spring 4 to Spring 5 lead to failed Junits using SpringJUnit4ClassRunner
- Intellij Test Case not Exiting
- Pre loading data from .sql before unit testing throws syntax error
- SpringBoot integration tests failing with java.awt.HeadlessException
- Configuration of SpringJUnit4ClassRunner Test clashes with SpringBootTest
- Spring 4, JUnit 4 without spring boot NoSuchBeanDefinitionException: No qualifying bean of type
- Spring configuration via code vs. configuration via annotations
- spring contextconfiguration using bean several times
- when to create objects using new operator or use auto-wire when testing a class?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Actually there are two ways to achieve your target:
To be used for real env:
To be used for tests :
In your web.xml specify default-spring-config.xml as the contextConfigLocation to be used by spring in runtime :
And finally specify test-spring-config.xml in ContextConfiguration for Test :
First of all add those lines to your web.xml to let Spring be aware of the default profile (real-env):
Now into your Spring config file (one single config file) create two different datasources :
Then inject DataSource into your test classes using ActiveProfiles:
Source : https://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles