A Little Java Feature Toggle Test Tool Introduction

[b]Toggle[/b]
Feature Toggle is common concept in the current software development process, especially if you want to do continous dilivery. But there is a pain for developer to test both Feature Toggle On and Off logic. Because most of Feature Toggle is just a Enum, It's static, It's hard to change the status of one Feature Toggle dynamicly. That's why i write this project. Toggle is a little java tool, which can help you easily test both Feature Toggle on and off logic in one test class.

[b]How-TO
[/b]
I'm trying to push this project into Maven central repository, Now it's waiting aprroval from Sonatype. when it's done, we can easily using maven depency include this little tool.

[b]step1: Include This Tool Into Your Project[/b]

There are three ways to include this tool into your project.

1. Using Maven depency(coming soon)
2. Download the project directly (Download) and add build/libs/Toggle.jar into your project classpath
3. Clone the source codes and add them into your project.

[b]step2:Define Your Own Toggle Class[/b]

Your feature toggle class must implements interface Toggle and must using ToggleStatus enum as the switch of Feature Toggle.

public enum MyFeatureToggle implements Toggle {
TEST_FEATURE(ToggleStatus.ON), STAGE1(ToggleStatus.ON), STAGE2(ToggleStatus.OFF);
private ToggleStatus status;

MyFeatureToggle(ToggleStatus status) {
this.status = status;
}

@Override
public void setStatus(ToggleStatus status) {
this.status = status;
}

public boolean isEnabled() {
return status.equals(ToggleStatus.ON);
}

@Override
public ToggleStatus getStatus() {
return status;
}

}


[b]step3: Apply Feature Toggle Test Rule To Your Test Class[/b]

Make sure the ToggleRule property is public.

@Rule
public ToggleRule anotherToggleRule = new ToggleRule();


[b]step4: Add Annotation On the Test Method To Decide The Toggle Status When Run This Test
[/b]
Now, this annotation support single, multiple and mixed Toggles.

@Test
@ToggleEnabled(toggleClass = "example.MyFeatureToggle",toggleNames = "TEST_FEATURE")
public void testEchoToggleOn() throws Exception {
assertThat(server.echo("xianlinbox"), is("Toggle is on, xianlinbox"));
}


@Test
@ToggleEnabled(toggleClass = "example.MyFeatureToggle",toggleNames = {"STAGE1","STAGE2"})
public void testMultipleToggle() throws Exception {
assertThat(server.multipleToggleEcho("xianlinbox"), is("TestFeature is on, Stage 1 is on, Stage 2 is on, xianlinbox"));
}

@Test
@ToggleDisabled(toggleClass = "example.MyFeatureToggle",toggleNames = {"STAGE1","TEST_FEATURE"})
@ToggleEnabled(toggleClass = "example.MyFeatureToggle",toggleNames = "STAGE2")
public void testMixedToggle() throws Exception {
assertThat(server.multipleToggleEcho("xianlinbox"), is("Stage 2 is on, xianlinbox"));
}

@Test
@ToggleDisabled(toggleClass = "example.MyFeatureToggle", toggleNames = {"STAGE1", "TEST_FEATURE"})
@ToggleEnabled(toggleClass = "example.MyFeatureToggle2", toggleNames = "TEST_FEATURE_2")
public void testMixedMultipleToggle() throws Exception {
assertThat(server.multipleToggleEcho("xianlinbox"), is("TestFeature 2 is on, xianlinbox"));
}

That's all , Hope you enjoy it.

Source Code: https://github.com/xianlinbox/Toggle
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值