|
| 1 | +# JUnit Open Feature extension |
| 2 | + |
| 3 | +A JUnit5 extension to reduce boilerplate code for testing code which utilizes OpenFeature. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +We are supporting two different flavors for testing, a [simple](#simple-configuration) and an [extended](#extended-configuration) configuration. |
| 8 | + |
| 9 | +Notice: We are most likely not multithread compatible! |
| 10 | +### Simple Configuration |
| 11 | + |
| 12 | +Choose the simple configuration if you are only testing in one domain. |
| 13 | +Per default, it will be used in the global domain. |
| 14 | + |
| 15 | +```java |
| 16 | +@Test |
| 17 | +@Flag(name = "BOOLEAN_FLAG", value = "true") |
| 18 | +void test() { |
| 19 | + // your test code |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +#### Multiple flags |
| 24 | + |
| 25 | +The `@Flag` annotation can be also repeated multiple times. |
| 26 | + |
| 27 | +```java |
| 28 | +@Test |
| 29 | +@Flag(name = "BOOLEAN_FLAG", value = "true") |
| 30 | +@Flag(name = "BOOLEAN_FLAG2", value = "true") |
| 31 | +void test() { |
| 32 | + // your test code |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +#### Defining Flags for a whole test-class |
| 37 | + |
| 38 | +`@Flags` can be defined on the class-level too, but method-level |
| 39 | +annotations will supersede class-level annotations. |
| 40 | + |
| 41 | +```java |
| 42 | +@Flag(name = "BOOLEAN_FLAG", value = "true") |
| 43 | +@Flag(name = "BOOLEAN_FLAG2", value = "false") |
| 44 | +class Test { |
| 45 | + @Test |
| 46 | + @Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used |
| 47 | + void test() { |
| 48 | + // your test code |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +#### Setting a different domain |
| 54 | + |
| 55 | +You can define your own domain on the test-class-level with `@OpenFeatureDefaultDomain` like: |
| 56 | + |
| 57 | +```java |
| 58 | +@OpenFeatureDefaultDomain("domain") |
| 59 | +class Test { |
| 60 | + @Test |
| 61 | + @Flag(name = "BOOLEAN_FLAG", value = "true") |
| 62 | + // this flag will be available in the `domain` domain |
| 63 | + void test() { |
| 64 | + // your test code |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Extended Configuration |
| 70 | + |
| 71 | +Use the extended configuration when your code needs to use multiple domains. |
| 72 | + |
| 73 | +```java |
| 74 | +@Test |
| 75 | +@OpenFeature({ |
| 76 | + @Flag(name = "BOOLEAN_FLAG", value = "true") |
| 77 | +}) |
| 78 | +@OpenFeature( |
| 79 | + domain = "domain", |
| 80 | + value = { |
| 81 | + @Flag(name = "BOOLEAN_FLAG2", value = "true") |
| 82 | + }) |
| 83 | +void test() { |
| 84 | + // your test code |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +#### Multiple flags |
| 90 | + |
| 91 | +The `@Flag` annotation can be also repeated multiple times. |
| 92 | + |
| 93 | +```java |
| 94 | +@Test |
| 95 | +@OpenFeature({ |
| 96 | + @Flag(name = "BOOLEAN_FLAG", value = "true"), |
| 97 | + @Flag(name = "BOOLEAN_FLAG2", value = "true") |
| 98 | +}) |
| 99 | +void test() { |
| 100 | + // your test code |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +#### Defining Flags for a whole test-class |
| 105 | + |
| 106 | +`@Flag` can be defined on the class-level too, but method-level |
| 107 | +annotations will superseded class-level annotations. |
| 108 | + |
| 109 | +```java |
| 110 | +@OpenFeature({ |
| 111 | + @Flag(name = "BOOLEAN_FLAG", value = "true"), |
| 112 | + @Flag(name = "BOOLEAN_FLAG2", value = "false") |
| 113 | +}) |
| 114 | +class Test { |
| 115 | + @Test |
| 116 | + @OpenFeature({ |
| 117 | + @Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used |
| 118 | + }) |
| 119 | + void test() { |
| 120 | + // your test code |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +#### Setting a different domain |
| 126 | + |
| 127 | +You can define an own domain for each usage of the `@OpenFeature` annotation with the `domain` property: |
| 128 | + |
| 129 | +```java |
| 130 | +@Test |
| 131 | +@OpenFeature( |
| 132 | + domain = "domain", |
| 133 | + value = { |
| 134 | + @Flag(name = "BOOLEAN_FLAG2", value = "true") // will be used |
| 135 | +}) |
| 136 | + // this flag will be available in the `domain` domain |
| 137 | +void test() { |
| 138 | + // your test code |
| 139 | +} |
| 140 | +``` |
| 141 | + |
0 commit comments