I have created a web application in spring-boot. I am writing a unit tests with testNG for my business layer.
I have created Application class
@SpringBootApplication
public class TestApplication
{
public static void main(String[] args)
{
SpringApplication.run(TestApplication.class, args);
}
@Bean
Mapper mapper()
{
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer-mappings.xml");
return new DozerBeanMapper(mappingFiles);
}
}
My test classes are looks like
@ContextConfiguration(classes = { TestApplication.class })
public class CommissionRuleServiceTest extends AbstractTestNGSpringContextTests
{
@InjectMocks
@Autowired
MyService
@Mock
MyDAO;
@BeforeMethod
public void initMock()
{
MockitoAnnotations.initMocks(this);
}
@Test(dataProvider = "....")
......
......
}
When I run project it shows hugh log on console and it takes times say 20.00secs for just few small tests. Some of statements from log are,
DEBUG o.s.c.i.s.PathMatchingResourcePatternResolver - Searching directory DEBUG o.s.c.a.ConfigurationClassPostProcessor DEBUG o.s.c.a.ClassPathBeanDefinitionScanner DEBUG o.s.c.i.s.PathMatchingResourcePatternResolver DEBUG o.s.b.f.s.DefaultListableBeanFactory DEBUG o.a.c.b.converters.ArrayConverter DEBUG org.dozer.loader.xml.XMLParser DEBUG org.hibernate.cfg.SettingsFactory DEBUG o.h.cfg.annotations.CollectionBinder DEBUG o.h.cfg.annotations.TableBinder DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : MyEntity DEBUG o.s.b.f.s.DefaultListableBeanFactory DEBUG org.hibernate.SQL
Why it is taking such a "hugh" time? What should I need to do?
java. Spring Boot provides an easy way to write a unit test for Rest controller. With the help of SpringJUnit4ClassRunner and MockMVC, a web application context can be created to write unit test for Rest controller. First, we add the necessary annotations to our test class as in the previous test.
JUnit is an open-source framework used to trigger and write tests. TestNG is a Java-based framework that is an upgraded option for running tests. JUnit does not support to run parallel tests. TestNG can run parallel tests.
Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication .
The investigation:
The @SpringBootApplication
annotation is equivalent to the following annotations with default attributes:
@Configuration
- Indicates that the class contains one or more @Bean
methods. Plays together with @ComponentScan
.
@EnableAutoConfiguration
- Will attempt to guess and configure beans that you are likely to need. This might cause some performance penalty depending on your application.
@ComponentScan
- Configures component scanning. As the package is not defined, scanning will occur from the package of the class with this annotation.Without more code it is not possible to give you an accurate guess, but I think most of the performance penalty is caused by Spring Boot initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With