我正在使用mockito,我正在尝试从我的服务测试一个get方法,问题是测试正在调用真正的方法,因此它根本不起作用。接下来是我想测试的方法:
public Hours getHours(String id) throws IllegalArgumentException {
logger.debug("getting id {}", id);
try {
return repository.get(validateUuid(id).toString());
} catch (Exception e) {
logger.error("there was an issue querying with id {}", id, e);
publishError("get", e);
throw new IllegalArgumentException(
String.format("there was an issue querying with id {%s}", id));
}
}
private UUID validateUuid(String id) {
try {
return UUID.fromString(id);
} catch (Exception e) {
throw new IllegalArgumentException("the id given was was invalid", e);
}
}
这是我的测试代码:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.when;
class serviceTest {
@Mock
private Repository<Hours> repository;
private Service service;
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
service = new service();
}
@Test
void addHours() {
Hours hours = new Hours();
hours.setCorrelationId("a-988787888b712ijj-0997878");
hours.setPerner("720022");
given(repository.post(any(Hours.class), anyBoolean())).willReturn(hours);
Hours persistedHours = service.addHours(hours);
assertEquals(persistedHours, hours);
}
@Test
void getHours() {
String mockedUUID = UUID.randomUUID().toString();
Hours hours = new Hours();
Hours.setCorrelationId("a-988787888b712ijj-0997878");
when(repository.get(mockedUUID)).thenReturn(hours);
Hours persistedHours = service.getHours(mockedUUID);
assertEquals(persistedHours.getCorrelationId(), hours.getCorrelationId());
}
@Test
void getHousrAll() {
List<Hours> hoursList = new ArrayList<>();
PaginatedResult<Hours> result = new PaginatedResult<>();
result.setResult(hoursList);
given(repository.get(any(Query.class))).willReturn(result);
PaginatedResult<Hours> result2 = service.getHousrAll(new Query());
assertEquals(result , result2);
}
}
我还使用过:
given(banquetLaborHoursRepository.get(anyString())).willReturn(banquetLabourHours);
但是结果是一样的。 有什么想法吗?
最佳答案
这可以帮助你。通常,当您进行测试时,您会将依赖项设置为模拟对象。这里的存储库是 YourService 的依赖项。 @InjectsMock 基本上是将存储库模拟对象设置到您的服务中。
但是如果您不想使用注释,您需要创建一个服务的模拟对象和存储库的模拟对象,并将其设置为服务模拟对象。这也可以工作,但是像 InjectsMock 这样的注释可以减少我们的代码。
@RunWith(MockitoJUnitRunner.class)
class GameTester {
@InjectMocks
YourService service;
@Mock
Repository repository;
@Test
void getHours() {
Hours hours = new Hours();
hours.setCorrelationId("a-988787888b712ijj-0997878");
when(repository.get(mockedUUID)).thenReturn(hours);
Hours persistedBanquet = service.getHours("a-988787888b712ijj-0997878");
assertEquals(persistedBanquet.getCorrelationId(), hours.getCorrelationId());
}
}
关于java - 模拟响应不起作用,并调用真正的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59935905/