测试Spring @Bean方法

时间:2019-06-10 14:17:47

标签: java spring junit mockito

我在Spring生态系统中进行开发,并且有2种方法可以测试。

@Configuration
@ComponentScan( "com.ratepay.iris.ella" )
public class EllaConfiguration {

private PostConnectionConfiguration<EllaResponseDto> createConnectionConfiguration( final Environment env ) {
        return new SimplePostConnectionConfiguration<>( env.getRequiredProperty( URI_CONFIG_KEY ), EllaResponseDto.class );
    }


    private SimpleTimeoutConfiguration createTimeoutConfiguration( final Environment env ) {
        return new SimpleTimeoutConfiguration( env.getProperty( CONNECTION_TIMEOUT_CONFIG_KEY, Integer.class, DEFAULT_CONNECTION_TIMEOUT ),
                                               env.getProperty( READ_TIMEOUT_CONFIG_KEY, Integer.class, DEFAULT_READ_TIMEOUT ) );
    }




    // I have the timeoutConfiguration method here 

}

我有第三个方法(在同一个类中),该方法使用最初提供的2个方法。

@Bean( name = ELLA_CONNECTOR_BEAN_NAME )
        public EntityServiceConnectable<EllaResponseDto> timeoutConfiguration( final Environment env ) {
            return ServiceConnectorBuilder.create( createConnectionConfiguration( env ) ).timeout( createTimeoutConfiguration( env ) ).build();
        }

我最初提供的2种方法的测试是

@Test
public void testCreateConnectionConfigurationReturnsCorrectConnectionConfig() throws Exception {

    EllaConfiguration elc = spy( EllaConfiguration.class );
    assertEquals( SimplePostConnectionConfiguration.class,
                  ( Whitebox.invokeMethod( elc, "createConnectionConfiguration", environment ) ).getClass() );
}


@Test
public void testCreateTimeoutConfigurationCreatesCorrectTimeoutConfiguration() throws Exception {

    EllaConfiguration elc = spy( EllaConfiguration.class );
    assertEquals( SimpleTimeoutConfiguration.class,
                  ( Whitebox.invokeMethod( elc, "createTimeoutConfiguration", environment ) ).getClass() );
}

如何测试timeoutConfiguration注释附带的@Bean方法?

0 个答案:

没有答案