Skip to content

Commit

Permalink
feat: 使用mockito处理Http Interface Client
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed May 16, 2024
1 parent 56972b2 commit a56d329
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, String> result = service.java();

assertNotEquals(result.get("java-version"), System.getProperty("java.version"));
assertEquals(result.get("java-version"), "17");
assertTrue(Boolean.parseBoolean(result.get("mockito")));
}

}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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")));
}

}
Original file line number Diff line number Diff line change
@@ -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<String, String> result = service.spring();

assertNotEquals(result.get("spring-version"), SpringVersion.getVersion());
assertEquals(result.get("spring-version"), "1.0.0");
assertTrue(Boolean.parseBoolean(result.get("mockito")));
}

}

0 comments on commit a56d329

Please sign in to comment.