Skip to content

Commit 215eaa3

Browse files
committed
use mockito for junit
1 parent c285a80 commit 215eaa3

5 files changed

Lines changed: 255 additions & 126 deletions

File tree

authcenter/src/test/java/com/highgo/cloud/auth/controller/AuthControllerTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ public class AuthControllerTest {
2525

2626
@Test
2727
public void testUserRegister() {
28-
assertEquals(4, 2 + 2);
28+
assertEquals(4, 2 + 2);
2929
}
3030

31-
// @Test
32-
// public void testUserLogout() {
33-
// fail("Not yet implemented");
34-
// }
35-
//
36-
// @Test
37-
// public void testUpdateUserInfo() {
38-
// fail("Not yet implemented");
39-
// }
40-
//
41-
// @Test
42-
// public void testEmailVerificationCode() {
43-
// fail("Not yet implemented");
44-
// }
45-
//
46-
// @Test
47-
// public void testEmailVerificationCodeForRegister() {
48-
// fail("Not yet implemented");
49-
// }
31+
// @Test
32+
// public void testUserLogout() {
33+
// fail("Not yet implemented");
34+
// }
35+
//
36+
// @Test
37+
// public void testUpdateUserInfo() {
38+
// fail("Not yet implemented");
39+
// }
40+
//
41+
// @Test
42+
// public void testEmailVerificationCode() {
43+
// fail("Not yet implemented");
44+
// }
45+
//
46+
// @Test
47+
// public void testEmailVerificationCodeForRegister() {
48+
// fail("Not yet implemented");
49+
// }
5050

5151
}

cloudnative/pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
<version>1.0-SNAPSHOT</version>
2626
</dependency>
2727

28-
<dependency>
29-
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-test</artifactId>
31-
<scope>test</scope>
32-
</dependency>
33-
3428
<dependency>
3529
<groupId>org.springframework.boot</groupId>
3630
<artifactId>spring-boot-starter</artifactId>
@@ -317,7 +311,13 @@
317311
<artifactId>spring-boot-configuration-processor</artifactId>
318312
<optional>true</optional>
319313
</dependency>
320-
314+
<!-- begin add for junit for mock db -->
315+
<dependency>
316+
<groupId>com.h2database</groupId>
317+
<artifactId>h2</artifactId>
318+
<scope>test</scope>
319+
</dependency>
320+
<!-- end add for junit for mock db -->
321321
</dependencies>
322322

323323
<build>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.highgo.platform.configuration;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertThrows;
22+
23+
import java.util.Optional;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.mockito.InjectMocks;
28+
import org.mockito.Mock;
29+
import org.mockito.Mockito;
30+
import org.mockito.junit.jupiter.MockitoExtension;
31+
import org.mockito.junit.jupiter.MockitoSettings;
32+
import org.mockito.quality.Strictness;
33+
import org.springframework.transaction.annotation.Transactional;
34+
35+
import com.highgo.platform.apiserver.model.po.K8sClusterInfoPO;
36+
import com.highgo.platform.apiserver.repository.K8sClusterInfoRepository;
37+
import com.highgo.platform.exception.CommonException;
38+
39+
import io.fabric8.kubernetes.client.KubernetesClient;
40+
41+
@ExtendWith(MockitoExtension.class)
42+
@Transactional
43+
@MockitoSettings(strictness = Strictness.LENIENT)
44+
class K8sClientConfigurationTest {
45+
46+
@InjectMocks
47+
private K8sClientConfiguration k8sClientConfiguration;
48+
49+
@Mock
50+
private K8sClusterInfoRepository k8sClusterInfoRepository;
51+
52+
// k8s config
53+
private String configInfo = "apiVersion: v1\n" +
54+
"clusters:\n" +
55+
"- cluster:\n" +
56+
" certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJek1UQXhNREF5TlRjek0xb1hEVE16TVRBd056QXlOVGN6TTFvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBT0pDCkRoanhCL3BHUXkwZnJRRVJ2Zm1YbHpwV0dsWWErdDZkdlIwMi9SSnM4R3VaMmdoeU00VjJQc245UWNXVzc0dmMKR1lsZUljVWxWYzM5MDdCR2Q1alJpbVQrYTM3elNrQ3g4NXV0bmpwenpUbW5EYWg4S3BXTW5JZGIzbU5LZkVJeQpoUDF3amU4ODQ5R0VtRVdnUURNYkFlMVVZWmRvRFFiOTF0bFBTc1NYbW5rcHJrSjVIS0pDY0t2UWtONkxDdEJQCmUrZXlxWitrd1lDU2l3dUljaXVGREplNnpGRlRhT1RMV09ucStlbG12TDAzNXd3NGh2WnR3RHVLdERPT3RaQkoKSlZQSW5hWjF6dFN6ekFTNDNxZU00c29CbkNGU0FneGxGbzRoT1pmekZUbHdPaGx3MUFTVHBLT1Jxek5vdVJuTQphbElYb1RHd3VKVTJ3aHl6bHI4Q0F3RUFBYU5aTUZjd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZNSXg2anpuSWVXUHA1aTVSamQ0UWRHWGkrc1RNQlVHQTFVZEVRUU8KTUF5Q0NtdDFZbVZ5Ym1WMFpYTXdEUVlKS29aSWh2Y05BUUVMQlFBRGdnRUJBRWlXNW1RVVEzRU03dHJYUktQcgpSQXFTdmkwOUZCSmJHdTIwdGdocTNuWm9uOTVyMHJpRVBONUlLN1JoVElGZVZGUEY0cFIxOHR4V3JxY0EzdDhvCkRzT3JWQWViTkxJTVBocUVWMFBJMmxBUlE5RHNKVWk1bk1xS2ZvU2VkVVNrN2NGb1U2UVZOY2JlTDRVb2NjckMKRm1FclVLYVAzbXBSS3hDN3RuTk5nRFpzTVZWY3psK0hIM3ZYWmdGQm5JMHNlNmVGS0Y2WFhCRlJIM2UwamNhUApvY3laSzhhVE1nb0Z2aVJjNkpjQndXeXVvSXJybkFrbWJwMS9TZFBEdVV1UGxBa21FVVduR3ZxQWJVVDRHZW5WCjQ2R1B5WHozR3M5MFdRaVNLalRTamljVVVmTWswY01JTXVud1FlNHlHZUNnSWZsdzlJdFNNTlBSK2tKYWhDWUcKN3YwPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==\n"
57+
+
58+
" server: https://127.0.1.1:6443\n" +
59+
" name: kubernetes\n" +
60+
"contexts:\n" +
61+
"- context:\n" +
62+
" cluster: kubernetes\n" +
63+
" user: kubernetes-admin\n" +
64+
" name: kubernetes-admin@kubernetes\n" +
65+
"current-context: kubernetes-admin@kubernetes\n" +
66+
"kind: Config\n" +
67+
"preferences: {}\n" +
68+
"users:\n" +
69+
"- name: kubernetes-admin\n" +
70+
" user: ";
71+
72+
@Test
73+
void testGetAdminKubernetesClientById() {
74+
75+
// 1. 测试取不到cluster
76+
CommonException exception = assertThrows(CommonException.class, () -> {
77+
// 在这里调用该方法
78+
k8sClientConfiguration.getAdminKubernetesClientById("123456");
79+
});
80+
81+
// 可以对异常对象进行进一步的断言
82+
assertEquals("instance..nternal.error", exception.getMessage());
83+
84+
// 2. 测试取到cluster
85+
Mockito.when(k8sClusterInfoRepository.findByClusterId("1")).thenReturn(getK8sList());
86+
87+
KubernetesClient client = k8sClientConfiguration.getAdminKubernetesClientById("1");
88+
assertEquals("https://127.0.1.1:6443", client.getMasterUrl().toString());
89+
90+
}
91+
92+
@Test
93+
void testGetAdminKubernetesClientByConfig() {
94+
KubernetesClient client = k8sClientConfiguration.getAdminKubernetesClientByConfig(configInfo);
95+
assertEquals("https://127.0.1.1:6443", client.getMasterUrl().toString());
96+
}
97+
98+
private K8sClusterInfoPO getK8sClusterInfo() {
99+
100+
K8sClusterInfoPO cluster = new K8sClusterInfoPO();
101+
cluster.setClusterId("1");
102+
cluster.setClusterName("test");;
103+
cluster.setId("1");
104+
cluster.setConfig(configInfo);
105+
106+
return cluster;
107+
}
108+
109+
private Optional<K8sClusterInfoPO> getK8sList() {
110+
K8sClusterInfoPO cluster = getK8sClusterInfo();
111+
Optional<K8sClusterInfoPO> optionalCluster = Optional.of(cluster);
112+
113+
return optionalCluster;
114+
}
115+
}

cloudnative/src/test/java/com/highgo/platform/operator/service/impl/CrServiceImplTest.java

Lines changed: 96 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,100 +18,110 @@
1818
package com.highgo.platform.operator.service.impl;
1919

2020
import static org.junit.Assert.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.*;
21+
22+
import javax.annotation.Resource;
2223

2324
import org.junit.jupiter.api.Test;
2425

2526
class CrServiceImplTest {
2627

28+
@Resource(name = "crServiceImpl")
29+
CrServiceImpl crServiceImpl;
30+
2731
@Test
2832
void testCreateCr() {
29-
assertEquals(4, 2 + 2);
33+
assertEquals(4, 2 + 2);
34+
}
35+
36+
// @Test
37+
// void testApplyCr() {
38+
// fail("Not yet implemented");
39+
// }
40+
41+
@Test
42+
void testIsCrExist() {
43+
44+
// String clusterId = "123456";
45+
// String namespace = "nstest";
46+
// String crName = "test";
47+
// boolean result = crServiceImpl.isCrExist(clusterId, namespace, crName);
48+
//
49+
// assertFalse(result);
3050
}
3151

32-
// @Test
33-
// void testApplyCr() {
34-
// fail("Not yet implemented");
35-
// }
36-
//
37-
// @Test
38-
// void testIsCrExist() {
39-
// fail("Not yet implemented");
40-
// }
41-
//
42-
// @Test
43-
// void testPatchCrResource() {
44-
// fail("Not yet implemented");
45-
// }
46-
//
47-
// @Test
48-
// void testPatchCrStorage() {
49-
// fail("Not yet implemented");
50-
// }
51-
//
52-
// @Test
53-
// void testDeleteCr() {
54-
// fail("Not yet implemented");
55-
// }
56-
//
57-
// @Test
58-
// void testDeleteAllPod() {
59-
// fail("Not yet implemented");
60-
// }
61-
//
62-
// @Test
63-
// void testRestartDatabase() {
64-
// fail("Not yet implemented");
65-
// }
66-
//
67-
// @Test
68-
// void testNodeportSwitch() {
69-
// fail("Not yet implemented");
70-
// }
71-
//
72-
// @Test
73-
// void testCreateBackup() {
74-
// fail("Not yet implemented");
75-
// }
76-
//
77-
// @Test
78-
// void testApplyBackupPolicy() {
79-
// fail("Not yet implemented");
80-
// }
81-
//
82-
// @Test
83-
// void testDeleteBackup() {
84-
// fail("Not yet implemented");
85-
// }
86-
//
87-
// @Test
88-
// void testApplyConfigParam() {
89-
// fail("Not yet implemented");
90-
// }
91-
//
92-
// @Test
93-
// void testGetInstanceVOFromCR() {
94-
// fail("Not yet implemented");
95-
// }
96-
//
97-
// @Test
98-
// void testGetMasterPod() {
99-
// fail("Not yet implemented");
100-
// }
101-
//
102-
// @Test
103-
// void testRestore() {
104-
// fail("Not yet implemented");
105-
// }
106-
//
107-
// @Test
108-
// void testGetHgadminPort() {
109-
// fail("Not yet implemented");
110-
// }
111-
//
112-
// @Test
113-
// void testPatchCrUsers() {
114-
// fail("Not yet implemented");
115-
// }
52+
// @Test
53+
// void testPatchCrResource() {
54+
// fail("Not yet implemented");
55+
// }
56+
//
57+
// @Test
58+
// void testPatchCrStorage() {
59+
// fail("Not yet implemented");
60+
// }
61+
//
62+
// @Test
63+
// void testDeleteCr() {
64+
// fail("Not yet implemented");
65+
// }
66+
//
67+
// @Test
68+
// void testDeleteAllPod() {
69+
// fail("Not yet implemented");
70+
// }
71+
//
72+
// @Test
73+
// void testRestartDatabase() {
74+
// fail("Not yet implemented");
75+
// }
76+
//
77+
// @Test
78+
// void testNodeportSwitch() {
79+
// fail("Not yet implemented");
80+
// }
81+
//
82+
// @Test
83+
// void testCreateBackup() {
84+
// fail("Not yet implemented");
85+
// }
86+
//
87+
// @Test
88+
// void testApplyBackupPolicy() {
89+
// fail("Not yet implemented");
90+
// }
91+
//
92+
// @Test
93+
// void testDeleteBackup() {
94+
// fail("Not yet implemented");
95+
// }
96+
//
97+
// @Test
98+
// void testApplyConfigParam() {
99+
// fail("Not yet implemented");
100+
// }
101+
//
102+
// @Test
103+
// void testGetInstanceVOFromCR() {
104+
// fail("Not yet implemented");
105+
// }
106+
//
107+
// @Test
108+
// void testGetMasterPod() {
109+
// fail("Not yet implemented");
110+
// }
111+
//
112+
// @Test
113+
// void testRestore() {
114+
// fail("Not yet implemented");
115+
// }
116+
//
117+
// @Test
118+
// void testGetHgadminPort() {
119+
// fail("Not yet implemented");
120+
// }
121+
//
122+
// @Test
123+
// void testPatchCrUsers() {
124+
// fail("Not yet implemented");
125+
// }
116126

117127
}

0 commit comments

Comments
 (0)