JMeter, JUnit and Spring Java configuration - spring

Is it possible to run JMeter with the JUnit plugin/sampler and Spring Java configuration? When I try to do this, the Spring autowired beans are not being created and although the test case runs, because the beans have not been created, I get null pointer exceptions.
I am using the Spring annotation
#SpringJUnit4ClassRunner and #ContextConfiguration to configure the JUnit test (which works). The goal is to be able to write JUnit test cases that can be measured for performance using JMeter.

Yes, running JMeter with JUnit and Spring is a problem.
JMeter does not use standard JUnit runner; its specified at
User manual, point 7. There are subtle differences between standard JUnit test runners and JMeter's implementation. Because of this, #SpringJUnit4ClassRunner is becomes ignored. Workaround is to load beans as normal i.e using ApplicationContext.getBean().

Related

How to automate the keycloak setup while running Junit test cases?

I have some Junit test cases in my Spring Boot project. I am using keycloak for getting tokens on the basis of the username and password in order to perform authentication. Now instead of manually deploying the keycloak server while running Junit test cases, I want to automate the keycloak deployment step. I want to ask that is there any Java library through which I can easily start the keycloak, pass the json file that contains all the configuration related to the realm, clients and users and then after running all Junit test cases keycloak automatically stops? Is there any way of doing such thing while running Junit test cases easily?
If you start an external service, this are surely not unit-tests. This is not even integration testing. That's part of end-to-end testing.
With Spring Boot:
the first (unit) are #WebMvcTest (or #WebFluxTest for reactive app) when testing a #Controller or plain JUnit with #ExtendWith(SpringExtension.class) to #EnableMethodSecurity and auto-wire an instrumented instance of the secured #Component (#Service or #Repository with #PreAuthorise, #PostFilter and alike). All external service and all #Components (but the one under test) are mocked and so is the Authentication in the security-context.
the second (integration) are #SpringBootTest which wire together the application #Components (but external services are still mocked / stubbed). It is still possible to #AutoConfigureMockMvc (or #AutoConfigureWebTestClient) and as so, to keep using mocked Authentication.
the last is best to be done without Java (Protractor or whatever end-to-end testing tool)
I answered this subject many times already. To mention a few:
https://stackoverflow.com/a/75465772/619830
How to write unit test for SecurityConfig for spring security
https://stackoverflow.com/a/75376543/619830

Spring integration test has different behaviour when started via IntelliJ run configuration and via mvn verify

I am working on the Spring application based on Spring Boot 2.6.7 (Spring framework version 5.3.19). I noticed that when I run the integration test via IntelliJ run configuration (basically created only by the right clicking on the test method name and choosing Run test) then the same instance of the ApplicationContext is used in the integration test class and in the actual SpringBootApplication which is tested by the integration test.
But when the same integration test is executed from the command line via mvn verify command, then the different instance of the ApplicationContext is active in the Spring Boot Application from the one which is active in the integration test class.
That for example has a consequence that spring data repository which I added as a field to the integration test class with #SpyBean annotation is not not applied in the Spring boot application. In the applicationContext of the integration test class, that spring data repository is registered as a spy but in the application context of the Spring Boot Application there is no spy proxy but the regular repository.
On the other hand when the test is running via IntelliJ run configuration, the applicationContext is same everywhere and the spy bean is active in the Spring Boot Application flow.
So I want to achieve the same behavior when I run mvn verify as I achieve when I run the test from IntelliJ. Any ideas?

junit test case for spring-integration-file

Am newbie to Spring. I have a spring project created using spring-integration-file (spring boot- FTP,SFTP,NFS) which will transform the file from source path to destination path.
Have to write junit test cases to test the project. It will be helpful if someone share some documents/link to write junit testcases with #RunWith(SpringRunner.class) for spring-integration-file project
Thanks in advance
You can consult Spring Integration Samples project on the matter.
Also there are a lot of JUnit tests in the spring-integration-file module of the core project per se.

TestNG or Spring Test framework?

I have used TestNG+Rest Assured for rest api service testing(application is written based on Spring framework) and just want to know if it is better to use Spring test framework alone or integration TestNG+Spring test.
Thanks.
Use the combination of both.
TestNG will act as full featured test framework and Spring Test framework to mock spring beans (In other words, to get the out of the box spring support).

Using Spring Boot Configuration in a custom JUnit test runner that does not otherwise use Spring

I have a custom JUnit test runner that executes acceptance-level tests using a test specification format specific to my project. The system under test is using Spring Boot and takes advantage of its configuration facility. I'd like the tests to be able to read the same configuration files in the same way. Obviously, using Spring Boot Configuration itself is an answer.
I'd like to just use Spring Boot Configuration as a stand-alone library, but I'm willing to fire up Spring Boot if that's what it takes. I'm not in control of the top-level application - JUnit is. So, I don't know how to start Spring Boot when I get control inside my test runner.
I've looked at extending SpringJunit4ClassRunner but I can't keep it from looking for #Test annotations and failing when it doesn't find any. I've started to look into merging code from SpringJunit4ClassRunner into my custom runner. Before I go too far down that path, I'd appreciate input from the community.
It sounds like you simply want the application running for a standalone webservice testing. This can be done simply by scripting the "java -jar" command to run the spring boot application. However, I would question why you don't want to leverage the testing tools built into spring boot? You can fire up the entire spring boot application and write some very logical looking tests.
For example a rest api test case:
#Test
public void homePage() throwsException () {
mockMvc.perform(get("/readingList"))
.andExpect(status().isOk())
.andExpect(view().name("readingList"))
.andExpect((model().attribute("books", is(empty()))));
}

Resources