我具有以下所有我的具体单元测试类都扩展的抽象单元测试类:
@ExtendWith(SpringExtension.class)
//@ExtendWith(MockitoExtension.class)
@SpringBootTest(
classes = PokerApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
public abstract class AbstractUnitTests {
@MockBean
public RoundService roundService;
@MockBean
public RoundRepository roundRepository;
}
使用@ExtendWith(SpringExtension.class)
或之间有什么区别@ExtendWith(MockitoExtension.class)
?
我问,使用任何一个注释似乎都没有什么区别,并且它们在我的代码中都可以分别工作-允许我使用Junit5。那么为什么两者都起作用?
具体测试班:
@DisplayName("Test RoundService")
public class RoundsServiceTest extends AbstractUnitTests {
private static String STUB_USER_ID = "user3";
// class under test
@InjectMocks
RoundService roundService;
private Round round;
private ObjectId objectId;
@BeforeEach //note this replaces the junit 4 @Before
public void setUp() {
initMocks(this);
round = Mocks.round();
objectId = Mocks.objectId();
}
@DisplayName("Test RoundService.getAllRoundsByUserId()")
@Test
public void shouldGetRoundsByUserId() {
// setup
given(roundRepository.findByUserId(anyString())).willReturn(Collections.singletonList(round));
// call method under test
List<Round> rounds = roundService.getRoundsByUserId(STUB_USER_ID);
// asserts
assertNotNull(rounds);
assertEquals(1, rounds.size());
assertEquals("user3", rounds.get(0).userId());
}
}
相关的Build.gradle部分:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.2.RELEASE'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'junit:junit:4.12'
}
test {
useJUnitPlatform()
}
什么是Junit扩展
Junit 5扩展的目的是扩展测试类或方法的行为
在Junit 5扩展模型和@ExtendWith
注释上阅读:此处
SpringExtension将Spring TestContext Framework集成到JUnit 5的Jupiter编程模型中。
public class SpringExtension
extends Object
implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver{..}
此扩展等效于我们的JUnit4 MockitoJUnitRunner的JUnit Jupiter
public class MockitoExtension
extends java.lang.Object
implements BeforeEachCallback, AfterEachCallback, ParameterResolver{..}
可以看出,SpringExtension
实现了比扩展更多的扩展MockitoExtension
。
还@SpringBootTest
带有meta注释,@ExtendWith(SpringExtension.class)
这意味着每次使用扩展测试时SpringExtension
。@MockBean
是Spring测试框架的注解,与@ExtendWith(SpringExtension.class)
要观察差异,请尝试以下操作
ExtendWith
只要 MockitoExtension
@ExtendWith(MockitoExtension.class)
class TestServiceTest {
@MockBean
TestService service;
@Test
void test() {
assertNotNull(service); // Test will fail
}
}
ExtendWith
只要 SpringExtension
@ExtendWith(SpringExtension.class)
class TestServiceTest {
@MockBean
TestService service;
@Test
void test() {
assertNotNull(service); // Test succeeds
}
}
ExtendWith
既SpringExtension
和MockitoExtension
@ExtendWith(MockitoExtension.class)
@ExtendWith(SpringExtension.class)
class TestServiceTest {
@MockBean
TestService service;
@Test
void test() {
assertNotNull(service); // Test succeeds
}
}
这两种方法都可以在您的情况下使用,因为已@SpringBootTest
解释了测试类的注释。
要回答这个问题:什么时候使用@ExtendWith
Spring或Mockito?,
当测试需要Spring Test Context(用于自动装配bean /使用@MockBean
)以及JUnit 5的Jupiter编程模型use时@ExtendWith(SpringExtension.class)
。这也将通过TestExecutionListeners支持Mockito注释。
测试使用Mockito并需要JUnit 5的Jupiter编程模型支持时 @ExtendWith(MockitoExtension.class)
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句