js Invalid character used in text string (destroydrop » JavaScripts &raquo Tree).

博客提到在客户端的JS、HTML、WEB、ANDROID、IOS开发,以及服务端的JAVA开发中,人们常懒得处理warning错误,如JAVA里ArrayList<>具体类型常不写。

 

 

 

 

 

无论客户端JS,HTML,WEB,ANDROID,IOS,还是服务端JAVA,我们都存在懒得处理这种的warning错误。

JAVA  最经常的就是 ArrayList<>,里面具体类型都懒得写。

我们来为如下方法编写一个单元测试类: ```java @Override public String getChannelBot(final String userChannel) { if (!UserChannel.BRANCH.equals(userChannel) && !UserChannel.DIRECT.equals(userChannel) && !UserChannel.BRANCH_MIX.equals(userChannel)) { return null; } Map<String, String> channelBotMap = operationConfigService.getChannelBotMap(); return channelBotMap.get(userChannel); } ``` 该方法逻辑如下: - 如果 `userChannel` 不是 BRANCH、DIRECT 或 BRANCH_MIX,返回 `null` - 否则从 `operationConfigService.getChannelBotMap()` 获取 map,并从中返回对应的 bot 值 --- ## ✅ 测试目标 我们要验证以下几种情况: 1. 传入非法的 `userChannel`(如 `"invalid"`) → 返回 `null` 2. 传入合法的 `userChannel`(如 `UserChannel.BRANCH`),但 `channelBotMap` 中没有对应值 → 返回 `null` 3. 传入合法的 `userChannel`,且 `channelBotMap` 中有对应值 → 返回该值 --- ## ✅ 使用 Mockito 编写的测试类(JUnit 4) ```java import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class IntellQAServiceImplTest { @Mock private OperationConfigService operationConfigService; @InjectMocks private IntellQAServiceImpl intellQAService; // 假设 UserChannel 是个常量类 public static class UserChannel { public static final String BRANCH = "BRANCH"; public static final String DIRECT = "DIRECT"; public static final String BRANCH_MIX = "BRANCH_MIX"; } @Before public void setUp() { // 可以在这里初始化通用的 mock 行为 } @Test public void testInvalidUserChannel_ReturnsNull() { // Arrange String invalidChannel = "INVALID"; // Act String result = intellQAService.getChannelBot(invalidChannel); // Assert assertNull(result); } @Test public void testValidUserChannel_BotNotInMap_ReturnsNull() { // Arrange String channel = UserChannel.BRANCH; Map<String, String> emptyMap = new HashMap<>(); when(operationConfigService.getChannelBotMap()).thenReturn(emptyMap); // Act String result = intellQAService.getChannelBot(channel); // Assert assertNull(result); } @Test public void testValidUserChannel_BotExistsInMap_ReturnsBotValue() { // Arrange String channel = UserChannel.DIRECT; String expectedBot = "DIRECT_BOT"; Map<String, String> channelBotMap = new HashMap<>(); channelBotMap.put(channel, expectedBot); when(operationConfigService.getChannelBotMap()).thenReturn(channelBotMap); // Act String result = intellQAService.getChannelBot(channel); // Assert assertEquals(expectedBot, result); } } ``` --- ## 🔍 关键点说明 | 技术点 | 描述 | |--------|------| | `@RunWith(MockitoJUnitRunner.class)` | 使用 Mockito 提供的 Runner 支持注解注入 | | `@Mock` | 创建 `OperationConfigService` 的 mock 对象 | | `@InjectMocks` | 自动注入 mock 到 `IntellQAServiceImpl` 实例中 | | `when(...).thenReturn(...)` | 模拟 `getChannelBotMap()` 的返回值 | --- ## ✅ 补充建议:使用 JUnit 5 + MockitoExtension(可选) 如果你使用的是 JUnit 5,可以替换为: ```java import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import java.util.HashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) public class IntellQAServiceImplTest { ... } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

13805029595

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值