Skip to content

Commit ef5057d

Browse files
committed
feat: optimize trpc-container test
1 parent 4a7f3ac commit ef5057d

17 files changed

Lines changed: 205 additions & 188 deletions

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/YamlUtilsTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import java.util.HashMap;
1717
import java.util.List;
1818
import java.util.Map;
19-
import org.junit.After;
20-
import org.junit.Assert;
21-
import org.junit.Before;
22-
import org.junit.Test;
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
2323

2424

2525
/**
@@ -31,7 +31,7 @@ public class YamlUtilsTest {
3131

3232
private YamlUtils yamlUtils;
3333

34-
@Before
34+
@BeforeEach
3535
public void setUp() throws Exception {
3636
this.properties = new HashMap<>();
3737
properties.put("string", "string");
@@ -41,72 +41,72 @@ public void setUp() throws Exception {
4141
this.yamlUtils = new YamlUtils("");
4242
}
4343

44-
@After
44+
@AfterEach
4545
public void tearDown() throws Exception {
4646
properties.clear();
4747
}
4848

4949
@Test
5050
public void testGetString() {
5151
String string = yamlUtils.getString(properties, "string");
52-
Assert.assertEquals("string", string);
52+
Assertions.assertEquals("string", string);
5353

5454
properties.put("string", null);
5555

5656
try {
5757
yamlUtils.getString(properties, "string");
5858
} catch (Exception e) {
59-
Assert.assertTrue(e instanceof IllegalArgumentException);
59+
Assertions.assertTrue(e instanceof IllegalArgumentException);
6060
}
6161
}
6262

6363
@Test
6464
public void testGetInteger() {
6565
int integer = yamlUtils.getInteger(properties, "integer");
66-
Assert.assertEquals(integer, 10);
66+
Assertions.assertEquals(integer, 10);
6767

6868
properties.put("integer", null);
6969

7070
try {
7171
yamlUtils.getInteger(properties, "integer");
7272
} catch (Exception e) {
73-
Assert.assertTrue(e instanceof IllegalArgumentException);
73+
Assertions.assertTrue(e instanceof IllegalArgumentException);
7474
}
7575
}
7676

7777
@Test
7878
public void testGetBoolean() {
7979
boolean bool = yamlUtils.getBoolean(properties, "boolean");
80-
Assert.assertTrue(bool);
80+
Assertions.assertTrue(bool);
8181

8282
properties.put("boolean", null);
8383
try {
8484
yamlUtils.getBoolean(properties, "boolean");
8585
} catch (Exception e) {
86-
Assert.assertTrue(e instanceof IllegalArgumentException);
86+
Assertions.assertTrue(e instanceof IllegalArgumentException);
8787
}
8888
}
8989

9090
@Test
9191
public void testGetCollection() {
9292
Collection collection = yamlUtils.getCollection(properties, "collection");
93-
Assert.assertNotNull(collection);
93+
Assertions.assertNotNull(collection);
9494
properties.put("collection", null);
9595
try {
9696
yamlUtils.getBoolean(properties, "collection");
9797
} catch (Exception e) {
98-
Assert.assertTrue(e instanceof IllegalArgumentException);
98+
Assertions.assertTrue(e instanceof IllegalArgumentException);
9999
}
100100
}
101101

102102
@Test
103103
public void testGetStringList() {
104104
List<String> collection = yamlUtils.getStringList(properties, "collection");
105-
Assert.assertNotNull(collection);
105+
Assertions.assertNotNull(collection);
106106
try {
107107
yamlUtils.requireMap(Arrays.asList(1, 2), "key");
108108
} catch (Exception e) {
109-
Assert.assertTrue(e instanceof IllegalArgumentException);
109+
Assertions.assertTrue(e instanceof IllegalArgumentException);
110110
}
111111
}
112112
}

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentConfigurationTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import com.tencent.trpc.container.config.ApplicationConfigParser;
44
import com.tencent.trpc.core.extension.ExtensionLoader;
5-
import org.junit.After;
6-
import org.junit.Assert;
7-
import org.junit.Before;
8-
import org.junit.Test;
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
99

1010
public class EnvironmentConfigurationTest {
1111

1212
private Environment environment;
1313

14-
@Before
14+
@BeforeEach
1515
public void init() {
1616
System.setProperty("global.namespace", "${env_type_enhancer}");
1717
System.setProperty("server.app", "wechat");
@@ -34,7 +34,7 @@ public void init() {
3434
environment = new Environment(parser);
3535
}
3636

37-
@After
37+
@AfterEach
3838
public void teardown() {
3939
System.clearProperty("global.namespace");
4040
System.clearProperty("server.app");
@@ -57,6 +57,6 @@ public void teardown() {
5757
@Test
5858
public void testGetInternalProperty() {
5959
Object internalProperty = environment.getInternalProperty("server.app");
60-
Assert.assertEquals(internalProperty, "wechat");
60+
Assertions.assertEquals(internalProperty, "wechat");
6161
}
62-
}
62+
}

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentTest.java

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111

1212
package com.tencent.trpc.container.config.system;
1313

14-
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.assertNull;
16-
import static org.junit.Assert.assertTrue;
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertNull;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1717

1818
import com.tencent.trpc.container.config.ApplicationConfigParser;
1919
import com.tencent.trpc.core.common.ConfigManager;
2020
import com.tencent.trpc.core.extension.ExtensionLoader;
2121
import java.util.Map;
2222
import java.util.NoSuchElementException;
23-
import org.junit.After;
24-
import org.junit.Assert;
25-
import org.junit.Before;
26-
import org.junit.Test;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
2727

2828
/**
2929
* Environment test class
@@ -34,7 +34,7 @@ public class EnvironmentTest {
3434
private ConfigManager configManager;
3535
private ConfigManager otherConfigManager;
3636

37-
@Before
37+
@BeforeEach
3838
public void init() {
3939
System.setProperty("global.namespace", "${env_type_enhancer}");
4040
System.setProperty("server.app", "wechat");
@@ -59,7 +59,7 @@ public void init() {
5959
otherConfigManager = environment.parse();
6060
}
6161

62-
@After
62+
@AfterEach
6363
public void teardown() {
6464
System.clearProperty("global.namespace");
6565
System.clearProperty("server.app");
@@ -123,64 +123,86 @@ public void testOverrideConfig() {
123123
assertEquals(protocol, configManager.getClientConfig().getProtocol());
124124
}
125125

126-
@Test(expected = IllegalStateException.class)
126+
@Test
127127
public void testInteger() {
128-
environment.getInteger("global.namespace", 2);
128+
Assertions.assertThrows(IllegalStateException.class, () -> {
129+
environment.getInteger("global.namespace", 2);
130+
});
129131
}
130132

131133
@Test
132134
public void testBoolean() {
133135
environment.getBoolean("global.namespace", true);
134136
}
135137

136-
@Test(expected = IllegalStateException.class)
138+
@Test
137139
public void testShort() {
138-
environment.getShort("global.namespace", (short) 2);
140+
Assertions.assertThrows(IllegalStateException.class, () -> {
141+
environment.getShort("global.namespace", (short) 2);
142+
});
139143
}
140144

141-
@Test(expected = IllegalStateException.class)
145+
@Test
142146
public void testByte() {
143-
environment.getByte("global.namespace", (byte) 2);
147+
Assertions.assertThrows(IllegalStateException.class, () -> {
148+
environment.getByte("global.namespace", (byte) 2);
149+
});
144150
}
145151

146-
@Test(expected = IllegalStateException.class)
152+
@Test
147153
public void testFloat() {
148-
environment.getFloat("global.namespace", 2f);
154+
Assertions.assertThrows(IllegalStateException.class, () -> {
155+
environment.getFloat("global.namespace", 2f);
156+
});
149157
}
150158

151-
@Test(expected = IllegalStateException.class)
159+
@Test
152160
public void testDouble() {
153-
environment.getDouble("global.namespace", 2d);
161+
Assertions.assertThrows(IllegalStateException.class, () -> {
162+
environment.getDouble("global.namespace", 2d);
163+
});
154164
}
155165

156-
@Test(expected = NoSuchElementException.class)
166+
@Test
157167
public void testIntNoElement() {
158-
environment.getInt("global.namespace.not.exist");
168+
Assertions.assertThrows(NoSuchElementException.class, () -> {
169+
environment.getInt("global.namespace.not.exist");
170+
});
159171
}
160172

161-
@Test(expected = NoSuchElementException.class)
173+
@Test
162174
public void testBooleanNoElement() {
163-
environment.getBoolean("global.namespace.not.exist");
175+
Assertions.assertThrows(NoSuchElementException.class, () -> {
176+
environment.getBoolean("global.namespace.not.exist");
177+
});
164178
}
165179

166-
@Test(expected = NoSuchElementException.class)
180+
@Test
167181
public void testShortNoElement() {
168-
environment.getShort("global.namespace.not.exist");
182+
Assertions.assertThrows(NoSuchElementException.class, () -> {
183+
environment.getShort("global.namespace.not.exist");
184+
});
169185
}
170186

171-
@Test(expected = NoSuchElementException.class)
187+
@Test
172188
public void testByteNoElement() {
173-
environment.getByte("global.namespace.not.exist");
189+
Assertions.assertThrows(NoSuchElementException.class, () -> {
190+
environment.getByte("global.namespace.not.exist");
191+
});
174192
}
175193

176-
@Test(expected = NoSuchElementException.class)
194+
@Test
177195
public void testFloatNoElement() {
178-
environment.getFloat("global.namespace.not.exist");
196+
Assertions.assertThrows(NoSuchElementException.class, () -> {
197+
environment.getFloat("global.namespace.not.exist");
198+
});
179199
}
180200

181-
@Test(expected = NoSuchElementException.class)
201+
@Test
182202
public void testDoubleNoElement() {
183-
environment.getDouble("global.namespace.not.exist");
203+
Assertions.assertThrows(NoSuchElementException.class, () -> {
204+
environment.getDouble("global.namespace.not.exist");
205+
});
184206
}
185207

186208
@Test
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.tencent.trpc.container.config.system;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
54

6-
import static org.junit.Assert.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
76

87
public class SystemConfigurationTest {
98

109
@Test
1110
public void testGetInternalProperty() {
12-
System.setProperty("testSysProperty","1");
11+
System.setProperty("testSysProperty", "1");
1312
SystemConfiguration systemConfiguration = new SystemConfiguration();
1413
Object result = systemConfiguration.getInternalProperty("testSysProperty");
1514
assertEquals("1", result);
1615
}
17-
}
16+
}

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/parser/DefaultPropertySourceParserTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
import com.tencent.trpc.container.config.system.Configuration;
44
import com.tencent.trpc.core.utils.YamlParser;
55
import java.util.Map;
6-
import junit.framework.TestCase;
7-
import org.junit.Assert;
8-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.Assertions;
98

10-
public class DefaultPropertySourceParserTest extends TestCase {
9+
public class DefaultPropertySourceParserTest {
1110

1211
@Test
1312
public void testGetFlattableMap() {
1413
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
1514
DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser();
1615
Map<String, Object> flattableMap = propertySourceParser.getFlattableMap(yamlConfigMap);
17-
Assert.assertEquals(147, flattableMap.size());
16+
Assertions.assertEquals(147, flattableMap.size());
1817
}
1918

2019
@Test
2120
public void testParseFlattableMap() {
2221
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
2322
DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser();
2423
Map<String, Object> stringObjectMap = propertySourceParser.parseFlattableMap(yamlConfigMap);
25-
Assert.assertEquals(4, stringObjectMap.size());
24+
Assertions.assertEquals(4, stringObjectMap.size());
2625
Boolean aBoolean = Configuration.toBooleanObject(true);
27-
Assert.assertTrue(aBoolean);
26+
Assertions.assertTrue(aBoolean);
2827
}
29-
}
28+
}

trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/BackendConfigParserTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import com.tencent.trpc.core.common.config.BackendConfig;
1515
import com.tencent.trpc.core.utils.YamlParser;
16-
import org.junit.Assert;
17-
import org.junit.Test;
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.Test;
1818
import java.util.Arrays;
1919
import java.util.List;
2020
import java.util.Map;
@@ -27,18 +27,18 @@ public class BackendConfigParserTest {
2727
@Test
2828
public void testParseConfigMap() {
2929
BackendConfigParser backendConfigParser = new BackendConfigParser();
30-
Assert.assertNotNull(backendConfigParser);
30+
Assertions.assertNotNull(backendConfigParser);
3131
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class);
3232
List<Map<String, Object>> maps = Arrays.asList(yamlConfigMap);
3333
Map<String, BackendConfig> stringBackendConfigMap = BackendConfigParser.parseConfigMap(maps);
34-
Assert.assertNotNull(stringBackendConfigMap);
34+
Assertions.assertNotNull(stringBackendConfigMap);
3535
}
3636

3737
@Test
3838
public void testParseConfig() {
3939
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class);
4040
BackendConfig backendConfig = BackendConfigParser.parseConfig(yamlConfigMap);
41-
Assert.assertNotNull(backendConfig);
41+
Assertions.assertNotNull(backendConfig);
4242
}
4343

4444
}

0 commit comments

Comments
 (0)