From a56d3298d58c594d58459190cc90a795de212345 Mon Sep 17 00:00:00 2001 From: livk Date: Thu, 16 May 2024 22:05:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8mockito=E5=A4=84?= =?UTF-8?q?=E7=90=86Http=20Interface=20Client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../livk/http/service/JavaServiceTest.java | 55 ++++++++++++++++++ .../http/service/SpringBootServiceTest.java | 56 +++++++++++++++++++ .../livk/http/service/SpringServiceTest.java | 56 +++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/JavaServiceTest.java create mode 100644 spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringBootServiceTest.java create mode 100644 spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringServiceTest.java diff --git a/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/JavaServiceTest.java b/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/JavaServiceTest.java new file mode 100644 index 000000000..198daf413 --- /dev/null +++ b/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/JavaServiceTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2021-2024 spring-boot-extension the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.livk.http.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * @author livk + */ +@SpringBootTest +class JavaServiceTest { + + @MockBean + JavaService javaService; + + @Autowired + JavaService service; + + @BeforeEach + void setUp() { + when(javaService.java()).thenReturn(Map.of("java-version", "17", "mockito", "true")); + } + + @Test + void java() { + Map result = service.java(); + + assertNotEquals(result.get("java-version"), System.getProperty("java.version")); + assertEquals(result.get("java-version"), "17"); + assertTrue(Boolean.parseBoolean(result.get("mockito"))); + } + +} diff --git a/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringBootServiceTest.java b/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringBootServiceTest.java new file mode 100644 index 000000000..83137118e --- /dev/null +++ b/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringBootServiceTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2021-2024 spring-boot-extension the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.livk.http.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootVersion; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * @author livk + */ +@SpringBootTest +class SpringBootServiceTest { + + @MockBean + SpringBootService bootService; + + @Autowired + SpringBootService service; + + @BeforeEach + void setUp() { + when(bootService.springBoot()).thenReturn(Map.of("spring-boot-version", "1.0.0", "mockito", "true")); + } + + @Test + void springBoot() { + Map result = service.springBoot(); + + assertNotEquals(result.get("spring-boot-version"), SpringBootVersion.getVersion()); + assertEquals(result.get("spring-boot-version"), "1.0.0"); + assertTrue(Boolean.parseBoolean(result.get("mockito"))); + } + +} diff --git a/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringServiceTest.java b/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringServiceTest.java new file mode 100644 index 000000000..8986708d7 --- /dev/null +++ b/spring-boot-extension-tests/http-spring-boot-test/src/test/java/com/livk/http/service/SpringServiceTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2021-2024 spring-boot-extension the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.livk.http.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.SpringVersion; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * @author livk + */ +@SpringBootTest +class SpringServiceTest { + + @MockBean + SpringService springService; + + @Autowired + SpringService service; + + @BeforeEach + void setUp() { + when(springService.spring()).thenReturn(Map.of("spring-version", "1.0.0", "mockito", "true")); + } + + @Test + void spring() { + Map result = service.spring(); + + assertNotEquals(result.get("spring-version"), SpringVersion.getVersion()); + assertEquals(result.get("spring-version"), "1.0.0"); + assertTrue(Boolean.parseBoolean(result.get("mockito"))); + } + +}