-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
ApolloEnvironmentTest.java
75 lines (59 loc) · 2.27 KB
/
ApolloEnvironmentTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.ctrip.apollo.client;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith(MockitoJUnitRunner.class)
public class ApolloEnvironmentTest {
private ApolloEnvironment apolloEnvironment;
@Mock
private ApolloEnvironmentManager apolloEnvironmentManager;
@Before
public void setUp() throws Exception {
apolloEnvironment = spy(ApolloEnvironment.getInstance());
ReflectionTestUtils
.setField(apolloEnvironment, "apolloEnvironmentManager", apolloEnvironmentManager);
}
@Test
public void testInit() throws Exception {
apolloEnvironment.init();
verify(apolloEnvironmentManager, times(1)).init();
}
@Test
public void testGetProperty() throws Exception {
CompositePropertySource somePropertySource = mock(CompositePropertySource.class);
String someKey = "someKey";
String someValue = "someValue";
apolloEnvironment.updatePropertySource(somePropertySource);
when(somePropertySource.getProperty(someKey)).thenReturn(someValue);
String result = apolloEnvironment.getProperty(someKey);
assertEquals(someValue, result);
}
@Test
public void testGetPropertyWithDefaultValue() throws Exception {
CompositePropertySource somePropertySource = mock(CompositePropertySource.class);
String someKey = "someKey";
String someDefaultValue = "someDefault";
apolloEnvironment.updatePropertySource(somePropertySource);
when(somePropertySource.getProperty(someKey)).thenReturn(null);
String result = apolloEnvironment.getProperty(someKey, someDefaultValue);
assertEquals(someDefaultValue, result);
}
@Test(expected = IllegalStateException.class)
public void testGetPropertyWithNoPropertySource() throws Exception {
String someKey = "someKey";
apolloEnvironment.getProperty(someKey);
}
}