jeudi 31 octobre 2019

how don't create some beans in spring boot test?

In my application has a component,when applciation ready the component need to do read some data from other system,so when I test,I want this componet not be created。

@Component
@Slf4j
public class DeviceStatisticsSyncHandler {
    @EventListener
    public void handle(ApplicationReadyEvent event) {
        syncDeviceStatisticsDataSync();
    }

    @Value("${test.mode:false}")
    public  boolean serviceEnabled;
}

I can use condition to solve this,bu the other code reader need to understand,so I think this is not a very good method.

@EventListener(condition =  "@deviceStatisticsSyncHandler .isServiceEnabled()")
public void handle(ApplicationReadyEvent event) {
    syncDeviceStatisticsDataSync();
}

public  boolean isServiceEnabled() {
    return !serviceEnabled;
}

@Value("${test.mode:false}")
public  boolean serviceEnabled;

My application don't use Profile,is there has other method to solve this problem.

Spring Boot version:2.1.3

Aucun commentaire:

Enregistrer un commentaire