SpringBoot Mockito 依赖注入
1 添加依赖 2 @Mock @MockBean @InjectMocks 3 @ExtendWith(MockitoExtension.class) @RunWith(MockitoJUnitRunner.class) 4 Mockito 单元测试 4.1 JunitTestService 4.2 JunitTestServiceImpl 4.3 TestService 4.4 TestServiceImpl 4.5 MockTest
1 添加依赖
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.13.2</ version>
< scope> test</ scope>
</ dependency>
2 @Mock @MockBean @InjectMocks
注解 作用 @Mock @Mock 注解用于创建一个模拟对象。它通常与 JUnit 的 @RunWith(MockitoJUnitRunner.class) 注解一起使用。在测试用例运行之前,Mockito 会自动初始化使用 @Mock 注解标注的对象。 @MockBean 注解类似于 @Mock,但它是在 Spring 测试环境中使用的。使用 @MockBean 可以添加一个模拟的 Bean 到 Spring 应用程序上下文中。通常与 @SpringBootTest 或 @WebMvcTest 注解一起使用。 @InjectMocks @InjectMocks 注解用于自动注入模拟对象到被测试对象中。它必须与 @Mock 或 @MockBean 注解一起使用。Mockito 会先初始化所有的模拟对象,然后将它们注入到使用 @InjectMocks 注解标注的被测试对象中。
注解 注入 @InjectMocks 需要使用@Mock标注的类,@MockBean会为空。 @Autowired 需要使用@MockBean标注的类,@Mock会为空。
3 @ExtendWith(MockitoExtension.class) @RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class) 是 Mockito 框架从 JUnit 5 开始引入的一种新的测试扩展机制。它取代了之前在 JUnit 4 中使用的 @RunWith(MockitoJUnitRunner.class) 注解。 MockitoExtension 是 JUnit 5 提供的一个扩展接口,Mockito 实现了这个接口,使 Mockito 能够集成到 JUnit 5 测试框架中。通过使用 @ExtendWith(MockitoExtension.class) 注解,可以在 JUnit 5 测试中使用 Mockito 的模拟功能。 使用 @ExtendWith(MockitoExtension.class) 注解后,你就可以直接使用 @Mock、@Spy、@Captor 等 Mockito 注解来创建和管理模拟对象,而不需要手动初始化这些模拟对象。同时,也不再需要使用 @RunWith 注解。
4 Mockito 单元测试
4.1 JunitTestService
package com. xu. test. service ;
public interface JunitTestService {
String test ( String a) ;
}
4.2 JunitTestServiceImpl
package com. xu. test. service. impl ;
import com. xu. test. service. JunitTestService ;
import org. springframework. stereotype. Service ;
@Service
public class JunitTestServiceImpl implements JunitTestService {
@Override
public String test ( String a) {
return a. concat ( " Mockito" ) ;
}
}
4.3 TestService
package com. xu. test. service ;
import jakarta. servlet. http. HttpServletRequest ;
import jakarta. servlet. http. HttpServletResponse ;
public interface TestService {
String test1 ( String a, Integer b) ;
void test2 ( HttpServletRequest request, HttpServletResponse response, String a, Integer b) ;
}
4.4 TestServiceImpl
package com. xu. test. service. impl ;
import cn. hutool. core. util. StrUtil ;
import com. xu. test. service. JunitTestService ;
import com. xu. test. service. TestService ;
import jakarta. annotation. Resource ;
import jakarta. servlet. http. HttpServletRequest ;
import jakarta. servlet. http. HttpServletResponse ;
import org. springframework. stereotype. Service ;
@Service
public class TestServiceImpl implements TestService {
@Resource
private JunitTestService testService;
@Override
public String test1 ( String a, Integer b) {
if ( StrUtil . equals ( "test" , a) ) {
testService. test ( a) ;
return "b" ;
}
return "a" ;
}
@Override
public void test2 ( HttpServletRequest request, HttpServletResponse response, String a, Integer b) {
if ( StrUtil . equals ( "test" , a) ) {
System . out. println ( a) ;
}
System . out. println ( b) ;
}
private static String getNewStr ( String a) {
return a. concat ( "单元测试" ) ;
}
}
4.5 MockTest
package com. xu. test ;
import com. xu. test. service. JunitTestService ;
import com. xu. test. service. TestService ;
import com. xu. test. service. impl. TestServiceImpl ;
import org. junit. Before ;
import org. junit. Test ;
import org. junit. jupiter. api. extension. ExtendWith ;
import org. mockito. InjectMocks ;
import org. mockito. Mock ;
import org. mockito. Mockito ;
import org. mockito. MockitoAnnotations ;
import org. mockito. junit. jupiter. MockitoExtension ;
@ExtendWith ( MockitoExtension . class )
public class MockTest {
@Mock
private JunitTestService testService;
@InjectMocks
public TestService userService = new TestServiceImpl ( ) ;
@Before
public void setUp ( ) {
MockitoAnnotations . openMocks ( this ) ;
}
@Test
public void testMock ( ) {
Mockito . when ( userService. test1 ( "test" , 10 ) ) . thenReturn ( "a" ) ;
}
}