如何为指定的meetsPreconditions业务方法编写JUnit测试用例
JUnit测试用例实现(基于JUnit 5 + Mockito)
该测试用例覆盖了meetsPreconditions方法的所有逻辑分支,通过Mock依赖组件解耦外部依赖,无需依赖真实业务数据即可完成单元测试。
前置依赖
需要在pom.xml(Maven)或build.gradle(Gradle)中引入JUnit 5和Mockito的依赖,示例Maven依赖如下:
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.11.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>4.11.0</version> <scope>test</scope> </dependency>
测试用例代码
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) public class YourTargetClassTest { // 替换为你实际的业务类名 @Mock private Model model; // 替换为你项目中实际的Model类名 @Mock private Toolkit toolkit; // 替换为你项目中实际的Toolkit类名 @Mock private DictionaryTools dictionaryTools; @Mock private LineFeature lineFeature; @Mock private DictionaryRangeItem formOfWayItem; @Mock private RoadElementsChain roadElementsChain; // 要测试的目标类,用Spy以便Mock内部调用的createRoadElementsChainBuilder方法 @Spy @InjectMocks private YourTargetClass target; // 替换为你实际的业务类名 @Test void meetsPreconditions_FormOfWayNotSmallSlipRoad_ReturnFalse() { // 构造FormOfWay不是SmallSlipRoad的场景 when(model.RANGES.FormOfWay.SmallSlipRoad).thenReturn("SMALL_SLIP_ROAD"); when(toolkit.getDictionaryTools()).thenReturn(dictionaryTools); when(dictionaryTools.getAttributeFromFeature(eq(lineFeature), any(), eq(DictionaryRangeItem.class))) .thenReturn(formOfWayItem); when(formOfWayItem.getValue()).thenReturn("NORMAL_ROAD"); boolean result = target.meetsPreconditions(lineFeature); assertFalse(result); // 验证不会进入链条构建逻辑 verify(target, never()).createRoadElementsChainBuilder(); } @Test void meetsPreconditions_LineFeaturesEmpty_ReturnFalse() { when(model.RANGES.FormOfWay.SmallSlipRoad).thenReturn("SMALL_SLIP_ROAD"); when(toolkit.getDictionaryTools()).thenReturn(dictionaryTools); when(dictionaryTools.getAttributeFromFeature(eq(lineFeature), any(), eq(DictionaryRangeItem.class))) .thenReturn(formOfWayItem); when(formOfWayItem.getValue()).thenReturn("SMALL_SLIP_ROAD"); when(target.createRoadElementsChainBuilder()).thenReturn(roadElementsChain); when(roadElementsChain.getLineFeatures()).thenReturn(List.of()); boolean result = target.meetsPreconditions(lineFeature); assertFalse(result); // 验证不会设置道路链条属性 verify(target, never()).setChainOfRoads(any()); } @Test void meetsPreconditions_StartFeaturesCountLessThan2_ReturnFalse() { when(model.RANGES.FormOfWay.SmallSlipRoad).thenReturn("SMALL_SLIP_ROAD"); when(toolkit.getDictionaryTools()).thenReturn(dictionaryTools); when(dictionaryTools.getAttributeFromFeature(eq(lineFeature), any(), eq(DictionaryRangeItem.class))) .thenReturn(formOfWayItem); when(formOfWayItem.getValue()).thenReturn("SMALL_SLIP_ROAD"); when(target.createRoadElementsChainBuilder()).thenReturn(roadElementsChain); when(roadElementsChain.getLineFeatures()).thenReturn(List.of(lineFeature)); when(roadElementsChain.getOtherLineFeaturesAtStart()).thenReturn(List.of(lineFeature)); // 数量为1,不满足>1的要求 when(roadElementsChain.getOtherLineFeaturesAtEnd()).thenReturn(List.of(lineFeature, lineFeature)); boolean result = target.meetsPreconditions(lineFeature); assertFalse(result); verify(target, never()).setChainOfRoads(any()); } @Test void meetsPreconditions_EndFeaturesCountLessThan2_ReturnFalse() { when(model.RANGES.FormOfWay.SmallSlipRoad).thenReturn("SMALL_SLIP_ROAD"); when(toolkit.getDictionaryTools()).thenReturn(dictionaryTools); when(dictionaryTools.getAttributeFromFeature(eq(lineFeature), any(), eq(DictionaryRangeItem.class))) .thenReturn(formOfWayItem); when(formOfWayItem.getValue()).thenReturn("SMALL_SLIP_ROAD"); when(target.createRoadElementsChainBuilder()).thenReturn(roadElementsChain); when(roadElementsChain.getLineFeatures()).thenReturn(List.of(lineFeature)); when(roadElementsChain.getOtherLineFeaturesAtStart()).thenReturn(List.of(lineFeature, lineFeature)); when(roadElementsChain.getOtherLineFeaturesAtEnd()).thenReturn(List.of(lineFeature)); // 数量为1,不满足>1的要求 boolean result = target.meetsPreconditions(lineFeature); assertFalse(result); verify(target, never()).setChainOfRoads(any()); } @Test void meetsPreconditions_AllConditionsMatch_ReturnTrueAndSetChain() { when(model.RANGES.FormOfWay.SmallSlipRoad).thenReturn("SMALL_SLIP_ROAD"); when(toolkit.getDictionaryTools()).thenReturn(dictionaryTools); when(dictionaryTools.getAttributeFromFeature(eq(lineFeature), any(), eq(DictionaryRangeItem.class))) .thenReturn(formOfWayItem); when(formOfWayItem.getValue()).thenReturn("SMALL_SLIP_ROAD"); when(target.createRoadElementsChainBuilder()).thenReturn(roadElementsChain); when(roadElementsChain.getLineFeatures()).thenReturn(List.of(lineFeature)); when(roadElementsChain.getOtherLineFeaturesAtStart()).thenReturn(List.of(lineFeature, lineFeature)); when(roadElementsChain.getOtherLineFeaturesAtEnd()).thenReturn(List.of(lineFeature, lineFeature)); boolean result = target.meetsPreconditions(lineFeature); assertTrue(result); // 验证链条构建和属性设置逻辑正常执行 verify(roadElementsChain, times(1)).buildRoadElementsChain(lineFeature); verify(target, times(1)).setChainOfRoads(roadElementsChain); } }
使用时将代码中占位的类名替换为你项目中对应的实际类名即可,如果model.RANGES.FormOfWay.SmallSlipRoad的返回值类型不是字符串,对应调整Mock返回值的类型即可。
内容的提问来源于stack exchange,提问作者deepankar gautam
相关产品推荐
相关产品推荐

