Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.mincong.reliability.featureflag;

import java.util.Set;

/**
* @author Mincong Huang
* @blog https://mincong.io/2020/11/11/feature-flag/
*/
public class MyJob {

private final Set<String> values;

public MyJob(Set<String> values) {
this.values = values;
}

public void run() {
var isNewFeatureEnabled = Boolean.getBoolean("NEW_FEATURE_ENABLED");
if (isNewFeatureEnabled) {
values.add("new");
} else {
values.add("old");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.mincong.reliability.featureflag;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashSet;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

/**
* @author Mincong Huang
* @blog https://mincong.io/2020/11/11/feature-flag/
*/
class MyJobTest {

@AfterEach
void tearDown() {
System.clearProperty("NEW_FEATURE_ENABLED");
}

@Test
void itShouldEnableNewFeature() {
// Given
System.setProperty("NEW_FEATURE_ENABLED", "True");
var set = new HashSet<String>();
var job = new MyJob(set);

// When
job.run();

// Then
assertThat(set).containsExactly("new");
}

@Test
void itShouldDisableNewFeature() {
// Given
System.setProperty("NEW_FEATURE_ENABLED", "False");
var set = new HashSet<String>();
var job = new MyJob(set);

// When
job.run();

// Then
assertThat(set).containsExactly("old");
}

@Test
void itShouldFallback() {
// Given
var set = new HashSet<String>();
var job = new MyJob(set);

// When
job.run();

// Then
assertThat(set).containsExactly("old");
}
}