I am trying to run some performance test cases on an spring application using jmeter.
I know that in jmeter, spring annotation would not work, like for example
#ContextConfiguration and #Autowired
which I over came by getting the ApplicationContext and then getting the Bean.
But, is there any other work around because I have a very big Base class which has lot of
#Autowired instance variables and lot of dependencies.
Thanks in Advance.
I'm not familiar with JMeter but I think you can treat your JMeter test cases as a standalone Spring application in which you load the ApplicationContext once then all your Spring Beans will be available.
See this tread: Using Spring 3 autowire in a standalone Java application
Hope this helps.
Related
I'm working on an integration with an older library so that it can use Spring Boot testing, and for that I need to register a certain bean very early in the process, so that an ApplicationListener<ApplicationReadyEvent> can add a PropertySource to the Environment.
This works fine in the normal startup, but when using the #SpringBootTest annotation I need to be able to inspect the TestContext very early and add it to the testing BootstrapContext, so that the application listener can access it also in the integration tests.
But I can't find a good way to add bean instances to the test BootstrapContext apart from specifying an initializer class in the spring.factories files, or am I missing something here?
I have been looking into using #BootstrapWith and subclassing SpringBootContextBootstrapper, but can't seem to find a way to add some kind of BootstrapregistryInitializer-like functions?
I am trying to write junit test cases for my Spring boot application written in 1.2.5.RELEASE version
I have read the spring boot documentation https://docs.spring.io/spring-boot/docs/1.2.5.RELEASE/reference/htmlsingle/ and trying to write the test case.
Now I want to mock a class which is already a #Primary class in original application.
see the issue here - How to mock a class that been annotated with Primary
On further read I understand that I need to use #MockBean to mock a class which is already a primary in application. But the problem is the spring boot version which I use does not support #MockBean.
I have tried overriding spring-boot-starter-test version alone with 2.1.7.RELEASE(without changing the spring boot parent version as I am not supposed to change the application spring boot version which has many impacts). Now I am able to use #MockBean, but on running the test case, it ends in exception.
java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getRepeatableAnnotations
How can I use #MockBean in spring boot 1.2.5.RELEASE?
Is there any workaround without using #MockBean?
I am trying to write integration test for SpringBoot application. code looks something like below
#RunWith(SpringRunner.class)
#SpringBootTest(classes = {Application.class, MyTestConfig.class})
#ActiveProfile("test")
class MyIntegrationTest {
#Autowire
ServiceInterface serviceA;
}
I noticed that applicationContext loads some of the service beans as Mockito mocked object which really defeats the purpose of Integration test as it does not execute some of the code. Can anyone suggest what could be wrong here. Please note that some of the services being autowired correctly but some are being mocked. I do not see any logical reason why they behaves differently since they are implemented same way. I am using spring boot 2.0.3
Already tried.
Removed MyTestConfig.class but problem remains same. Even if I use #SpringBootTest(classes = {Application.class, MyProblematicServiceImpl.class}), It still returns mocked object wherever it is autowired. MyProblematicServiceImpl is empty class annotated with #Service.
Looking at the docs, if you set the webEnvironment setting on the SpringBootTest annotation to something other than MOCK, the default, then it will start up a real web environment.
Promoting from the comments, so it’s answered.
The Application results in a component scan, which is picking up a test config you have. You may have to exclude some test configurations.
Spring Boot provides #TestConfiguration to solve this issue.
I am trying to implement integration tests for my Tomcat application, but my issue is that the application is launched separately from the tests so the tests cannot access the application context and neither the database.
My idea is running the tests "within" the running application, so I can #Autowire EntityManager and check for instance the state of the database during testing or even create database entities for testing.
My only idea of doing this is to actually run the application programmatically from the tests as ClassPathXmlApplicationContext("applicationContext.xml") and the access the Context. This would work, but it would be very hard for debugging as we wouldn't be able to use Hotswapping during the testing. Also I guess the server would be stopped as soon as the tests would end. I guess that is not the best and correct solution.
EDIT:
My question was probably unclear, so I will try to clarify.
I have a Tomcat application with Spring and Hibernate. The Spring beans and Hibernate database connection is initialised when the Tomcat application is started. The issue is how to run the tests of the active Spring beans from methods annotated with #Test in src/test/java which are started separately.
Consider this class:
#Component
class MyRepository {
#Autowired
EntityManager em;
#Transactional
public void myMethod(MyEntity entity) {
// do some job with entity
...
em.flush();
}
}
This class will be initialised with Tomcat as a MyRepository bean.
To test it, I cannot just call new MyRepository().myMethod(...) - I need to access the bean. The issue is accessing the bean from the #Test method:
#Test
void testMyRepository() {
Item item = ...
// then use the repository to handle the entity
context.getBean(MyRepository.class).myMethod(item);
// then assert the state of the database
context.getBean(EntityManager.class).find(Item.class, ...) ...
}
I can probably get the context in the initialisation of the tests with
ApplicationContext context = ClassPathXmlApplicationContext("applicationContext.xml");
But it would mean launching the whole application each time the tests are started. The better solution would be if the application could run separately from the tests.
Hope my problem is more clear now.
I would suggest you to use the SpringRunner to start the Spring application context and perform your tests on that running instance. You can customize the context the way it doesn't contain parts you don't want to tests and you can create mocks for components that require some external resources (REST clients and such). Take a look at the Spring docs or Spring Boot docs.
If multiple tests use the same Spring context configuration, the context is started just once and reused. So it's good to have it's configuration in a parent class of your tests. You can autowire any Spring bean into your test and test it.
You can use an in-memory database (such as H2) instead of a production one, so your tests are not dependent on an external infrastructure. To initialize the database, use tools like Flyway or Liquibase. To clear the database before each test, you can use the #Sql annotation.
You can find many examples of projects with such tests, for example my own demo.
If you want to test an external system, I would suggest something like JMeter.
Unfortunately you cant mirror your classes and use them in your tests. Thats a big disadvantage of web services. They always depend on user / machine interaction. With a lot of effort you can extract the functionality of the essential classes or methods and construct test scenarios etc. with jUnit.
The Overview of your possibilities:
special drivers and placeholders
you can use a logger with detailed log-level and file output. Then you created scenarios with the expected result and compare it with your log files.
Capture replay tools. They record your exection and replay them for monitoring.
I can also recommend using Selenium for the frontend tests.
Hope it helped.
I have a Spring service I want to test that uses the #Validated annotation on one of its method parameters. I would like to test it in the Spring container, but I am not sure if that is the best way. If testing in the container is the best solution for my situation, I would like to know how to run it in the container without loading my complete configuration. Any thoughts?
I ended up using the Spring Test Framework. I am using the standalone and the web application context of the mock MVC to test my annotations.
Spring 3.2.4 Test Framework
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html
Configuration Example
http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-configuration/