Mockito - 模拟自动装配服务
Mockito - Mock Autowired Service
我有一个 Rest 控制器 class,其中我正在自动装配服务层。现在我想在我的测试 class 中模拟服务层,但是在 运行 我的测试 class Wanted but not invoked: dbCacheService.refreshCache();
时出现以下异常
控制器代码
public class AppController {
@Autowired
private DbCacheService dbCacheService;
@GetMapping("/refresh")
@ApiOperation(value = "Refresh the database related cache", response = String.class)
public String refreshCache() {
dbCacheService.refreshCache();
return "DB Cache Refresh completed";
}
}
测试Class
@ExtendWith(MockitoExtension.class)
class SmsControllerTest {
@Mock
private DbCacheService dbCacheService;
@BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
void refreshCache() {
Mockito.verify(dbCacheService, Mockito.times(1)).refreshCache();
}
}
我是 JUnit5 和 Mockito 的新手。谁能告诉我哪里出错了?
您实际上并没有在测试中调用 Controller 方法,这就是为什么它抱怨从未发生对 dbCacheService.refreshCache()
的调用。尝试以下操作:
@ExtendWith(MockitoExtension.class)
class SmsControllerTest {
@Mock
private DbCacheService dbCacheService;
@InjectMocks
private AppController appController;
@BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
void refreshCache() {
appController.refreshCache();
Mockito.verify(dbCacheService, Mockito.times(1)).refreshCache();
}
}
虽然这可能有效,但这不是测试控制器的正确方法。您应该通过实际发出 HTTP 请求而不是方法调用来测试它们。为此,您需要使用 @WebMvcTest
:
进行切片测试
- https://www.baeldung.com/spring-boot-testing#unit-testing-with-webmvctest
- https://rieckpil.de/spring-boot-test-slices-overview-and-usage/
符合以下内容:
@RunWith(SpringRunner.class)
@WebMvcTest(AppController.class)
public class AppControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private DbCacheService dbCacheService;
@Test
public void refreshCache() {
mvc.perform(get("/refresh")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Mockito.verify(dbCacheService, Mockito.times(1)).refreshCache();
}
}
我有一个 Rest 控制器 class,其中我正在自动装配服务层。现在我想在我的测试 class 中模拟服务层,但是在 运行 我的测试 class Wanted but not invoked: dbCacheService.refreshCache();
控制器代码
public class AppController {
@Autowired
private DbCacheService dbCacheService;
@GetMapping("/refresh")
@ApiOperation(value = "Refresh the database related cache", response = String.class)
public String refreshCache() {
dbCacheService.refreshCache();
return "DB Cache Refresh completed";
}
}
测试Class
@ExtendWith(MockitoExtension.class)
class SmsControllerTest {
@Mock
private DbCacheService dbCacheService;
@BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
void refreshCache() {
Mockito.verify(dbCacheService, Mockito.times(1)).refreshCache();
}
}
我是 JUnit5 和 Mockito 的新手。谁能告诉我哪里出错了?
您实际上并没有在测试中调用 Controller 方法,这就是为什么它抱怨从未发生对 dbCacheService.refreshCache()
的调用。尝试以下操作:
@ExtendWith(MockitoExtension.class)
class SmsControllerTest {
@Mock
private DbCacheService dbCacheService;
@InjectMocks
private AppController appController;
@BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
void refreshCache() {
appController.refreshCache();
Mockito.verify(dbCacheService, Mockito.times(1)).refreshCache();
}
}
虽然这可能有效,但这不是测试控制器的正确方法。您应该通过实际发出 HTTP 请求而不是方法调用来测试它们。为此,您需要使用 @WebMvcTest
:
- https://www.baeldung.com/spring-boot-testing#unit-testing-with-webmvctest
- https://rieckpil.de/spring-boot-test-slices-overview-and-usage/
符合以下内容:
@RunWith(SpringRunner.class)
@WebMvcTest(AppController.class)
public class AppControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private DbCacheService dbCacheService;
@Test
public void refreshCache() {
mvc.perform(get("/refresh")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Mockito.verify(dbCacheService, Mockito.times(1)).refreshCache();
}
}