Force spring test configuration to load before main - java

I have a spring configuration class in main that creates redis client bean.
In tests I have another configuration class which creates embedded redis server bean. The problem is to force embedded redis server bean to be created before redis client one. I tried with #Order anotation without success. Importing test configuration to main one isn't an option.
When I define EmbeddedRedis as #Component then it will be created before the client but this is also not an option since EmbeddedRedis is a part of shareable test library which should not be created when not needed.
I have a workaround solution with an interface called RedisServerConfiguration which has different implementations for main and test and client depends on it so I am able to start server before the client tries to connect but this requires another abstraction and it would be nice to simplify it. Is there any spring mechanism I am not aware of that allows to force the order of bean initialization ?

Related

Testing with Embeded stub/dummy application in SpringBoot

I'm trying to write a integration test for my SpringBoot microservice that interacts with another service inside the product ecosystem.
Since this kind of testing is consider function/integration testing (depending on what nomenclature you use) it is usually done on some development environment.
However, I wanted to test a basic interaction between my service and a STUB/dummy app which are connected with RPC (so not exactly a typical TestRestTemplate test).
I know that there is a way to embed a service while booting up the Spring Context but have never done it by myself.
Does anyone have any experience with the upper or maybe a few helpful links where I can explore.
I have used WireMock in tests to mock services external to what I want to test that communicate over HTTP.
My test class annotated with #SpringBootTest is also annotated with #ContextConfiguration. In the classes attribute #ContextConfiguration I explicitly specify the configuration classes required to set up the Spring Context for the test in question. Here I can also include additional configuration classes in which I create beans only used in the test. In test configuration classes I can also override beans for the purpose of the test, creating mock beans etc.
Note that Spring Boot 2.1 and later disables bean overriding by default. It can be enabled by setting the following property to true:
spring.main.allow-bean-definition-overriding=true
To set the property for a single test, use the #TestPropertySource annotation like this:
#TestPropertySource(properties = {
"spring.main.allow-bean-definition-overriding=true"
})

Is there a way to use JMS in a JUnit test?

I want to run a JUnit test with usage of JMS. Is it possible to have a JUnit test use JMS outside of an Application Server like JBoss or a CDI container?
Provided that sending and consuming the message is completely decoupled from JMS, you could mock it.
For example: You can have a class that implements an interface like "IMyClassSender". In real code (non junit), all this class does is submit the message to JMS. In junit, implement IMyClassSender with a class that takes the input and instead if submitting to JMS, it passes it to your consumer class.
Alternatively, if you are using active mq: http://activemq.apache.org/how-to-unit-test-jms-code.html
You may also reconsider using an application server for this - Arquillian (http://arquillian.org) allows for executing unit and integration test within a JavaEE environment of your choice - and manages the app server lifecycle on its own.

How to update configuration of SpringBoot application at runtime without reloading whole ApplicationContext

I am trying to figure out how can I dynamically update/reload externalized configuration in a Spring Boot application without restarting the whole application.
Most of the advice involves reloading ApplicationContext after changing the externalized configuration, but that is equivalent to restarting the entire application, so this is not really all that useful.
Reading through SpringBoot reference documentation, I found a chapter 23.7 Typesafe Configuration Properties.
If I understand it correctly, this allows to define simple POJO classes that will hold your application (externalized) configuration values as attributes.
In theory at least, this scheme could be used to bind beans only once to the required configuration POJO and upon configuration change just update the values in the POJO. Components could easily pick up the changes next time they access getters on the POJO...
However, I have yet not managed to figure out how to enable this type of behavior. Is there some glaringly obvious way to dynamically update components annotated with #ConfigurationProperties when relevant configuration has changed?
It sounds like you're looking for #RefreshScope which is provided by Spring Cloud. From the Spring Cloud documentation:
A Spring #Bean that is marked as #RefreshScope will get special treatment when there is a configuration change. This addresses the problem of stateful beans that only get their configuration injected when they are initialized. For instance if a DataSource has open connections when the database URL is changed via the Environment, we probably want the holders of those connections to be able to complete what they are doing. Then the next time someone borrows a connection from the pool he gets one with the new URL.

Inject a bean from another application context?

Is it possible to inject a bean from a web application that deploy in another server!
I declare a scenario to myself, I have two web application that use spring framework and deploy separately in different application servers (one is TOMCAT and another one is WEBLOGIC),the first application has ServiceA and the second one has ServiceB, now I want to inject ServiceB in ServieA?
I try to do this with RMI once an another one with JMS, now I am wondering that:
Is it possible with another thing?
Is there any active project about this scenario exist?
How can share application context in spring framework, is it possible?
Thanks.
Bean is just an object in JVM. You certainly cannot use an object from one JVM in another JVM straightforward.
But you can do 2 things:
Use proxies - some objects that will have the same interface but invoke somehow to the proper server as implementation.
Use service-oriented architecture (SOA). Each server should have some limited set of beans that are responsible for their functionality. And all beans can interact with each other.
Maybe OSGI is suitable for this.
Web services, JAX-RS is the simplest. But JAX-WS provides you with the tools to automatically generate the client code.

FunctionalTestCase in Mule - How do I load one flow before another?

I have a Mule application, and I'm creating some unit tests including some Mock Web Services to test certain functionality. The Mock Web Services I'm creating using a test mule flow, so I'm registering an HTTP inbound endpoint.
When I run my test, I get a failure because one of my Spring objects fails to load due to the fact that it cannot reach my mock web service(The web service is loaded dynamically, so when doing the test, it resolves to localhost, otherwise it is usually an external service). I've made sure that when I do my getConfigResources() I load my mock web service flow file before I load my spring context beans, however I still get the error.
Is there a way to make Mule/Spring load my flow file and start up my http:inbound-endpoint before my Spring beans?
You could try setting the depends-on attribute on your Spring bean to the name of the Mule flow, but I doubt it will work and I'm not convinced it's something desirable anyway.
Otherwise, the only thing I can think of is that you initialize the Spring bean factory yourself in a #Before method of your functional test case. This will give you an opportunity to pass the value of the dynamic port you're using with your mock HTTP inbound endpoint (assuming you do use a dynamic port, which is the recommended approach).

Categories

Resources