mardi 19 décembre 2017

Spring Testing takes long time between two JUnit Suite test classes

i'm new to Spring Testing framework. I'm trying to do some integration tests, I have three test classes within a Junit Suite: TestClass1, TestClass2 and TestClass3, each of them having several @Test methods. I'm also using Mockito.

The problem is that when I execute the whole suite, it happens that TestClass1 is the first class to get executed (that's not a problem), but its last method takes a lot of time to execute, more than 10 seconds (if executed alone it takes order of millis), before the next test class methods start.

All these classes extend BaseTest which in turn extends AbstractTest. There are 2 configuration used: BaseConfig and ExtendedConfig.

I'm going to provide relevant snippets of code.

@WebAppConfiguration
@Transactional
public class AbstractTest {

    protected MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setUp() throws Exception {
        mockMvc =  MockMvcBuilders.webAppContextSetup(this.wac).apply(springSecurity()).build();
    }

    // other utility methods with mockMvc 
}


@ContextConfiguration(classes = BaseConfig.class)
public class BaseAbstractTest extends AbstractTest {

   // some @Autowired components and mocking methods

}


@ContextConfiguration(classes = ExtendedConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestClass1 extends BaseAbstractTest {

}


@RunWith(SpringJUnit4ClassRunner.class)
public class TestClass2 extends BaseAbstractTest {

}


@RunWith(SpringJUnit4ClassRunner.class)
public class TestClass3 extends BaseAbstractTest {

}


@Configuration
@ComponentScan(basePackages = { "com.base.package" })
@PropertySource("classpath:application-test.properties")
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class })
public class BaseConfig {

    @Mock
    protected RestClient1 RestClient1;

    @Mock
    protected AuditorAwareImpl auditorAwareImpl;

    public BaseConfig() {
        MockitoAnnotations.initMocks(this);
    }

    @Bean
    @Primary
    public RestClient1 restClient1() {
        return restClient1;
    }

    @Bean
    @Primary
    public AuditorAwareImpl auditorAware() {
        return auditorAwareImpl;
    }
}


public class ExtendedConfig {

    @Mock
    protected RestClient2 restClient2;

    public CustomTestStartupConfiguration() {
            MockitoAnnotations.initMocks(this);
    }

    @Bean
    @Primary
    private RestClient2 restClient2() {
        return restClient2;
    }


}

If I try to comment the annotation

@ContextConfiguration(classes = ExtendedConfig.class)

on the TestClass1 class (the only class that uses ExtendedConfig) I don't have this huge delay.

So I guess the issue is related to this but I can't understand why.

Is anyone able to help me by checking this code, if more information is needed just ask.

Thanks

Aucun commentaire:

Enregistrer un commentaire