Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test with testNG in spring boot takes time to build project

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?

like image 457
Prashant Shilimkar Avatar asked May 27 '15 10:05

Prashant Shilimkar


People also ask

What is the easiest method to write a unit test in spring?

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.

How is TestNG better than JUnit?

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.

Which annotation is used for unit testing in spring boot?

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 .


1 Answers

The investigation:

The @SpringBootApplication annotation is equivalent to the following annotations with default attributes:

  1. @Configuration - Indicates that the class contains one or more @Bean methods. Plays together with @ComponentScan.

  2. @EnableAutoConfiguration - Will attempt to guess and configure beans that you are likely to need. This might cause some performance penalty depending on your application.

  3. @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.

like image 129
Crazyjavahacking Avatar answered Oct 06 '22 00:10

Crazyjavahacking