11. verify验证方法调用

package lesson11;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class VerifyTest {

    @Mock
    private SimpleDao simpleDao;

    @Test
    public void testRealCallAdd() {
        SimpleService simpleService = new SimpleService(simpleDao);
        Simple simple = new Simple();
        when(simpleDao.exist(simple)).thenReturn(false);
        when(simpleDao.add(simple)).thenReturn(true);
        when(simpleDao.update(simple)).thenReturn(true);

        boolean result = simpleService.merge(simple);
        assertThat(true, equalTo(result));
        /**
         * 利用verify确定究竟调用的是哪个方法
         */
        verify(simpleDao, times(1)).add(simple);
        verify(simpleDao, times(0)).update(simple);
    }

    @Test
    public void testRealCallUpdate() {
        SimpleService simpleService = new SimpleService(simpleDao);
        Simple simple = new Simple();
        when(simpleDao.exist(simple)).thenReturn(true);
        when(simpleDao.add(simple)).thenReturn(true);
        when(simpleDao.update(simple)).thenReturn(true);

        boolean result = simpleService.merge(simple);
        assertThat(true, equalTo(result));
        verify(simpleDao, times(0)).add(simple);
        verify(simpleDao, times(1)).update(simple);
    }

    @Test
    public void testMerge() {
        SimpleService simpleService = new SimpleService(simpleDao);
        Simple simple = new Simple();
        when(simpleDao.exist(simple)).thenReturn(true, false);
        when(simpleDao.add(simple)).thenReturn(true);
        when(simpleDao.update(simple)).thenReturn(true);

        boolean result = simpleService.merge(simple);
        assertThat(true, equalTo(result));
        verify(simpleDao, times(0)).add(simple);
        verify(simpleDao, times(1)).update(simple);

        result = simpleService.merge(simple);
        assertThat(true, equalTo(result));
        verify(simpleDao, times(1)).add(simple);
        verify(simpleDao, times(1)).update(simple);
    }

    @After
    public void destroy() {
        reset(simpleDao);
    }
}
package lesson11;

public class SimpleService {
    public SimpleDao simpleDao;

    public SimpleService(SimpleDao simpleDao) {
        this.simpleDao = simpleDao;
    }

    public boolean merge(Simple simple) {
        boolean exist = simpleDao.exist(simple);
        if (exist) {
            return simpleDao.update(simple);
        } else {
            return simpleDao.add(simple);
        }
    }
}
SimpleService
package lesson11;

public class SimpleDao {
    public boolean exist(Simple simple) {
        throw new RuntimeException();
    }

    public boolean update(Simple simple) {
        throw new RuntimeException();
    }

    public boolean add(Simple simple) {
        throw new RuntimeException();
    }
}
SimpleDao
package lesson11;

public class Simple {
}
Simple
### 问题分析 在Java中使用Reactor和SignedJWT时,如果验证失败但未进入`if`语句,可能的原因包括以下几点: 1. **异常处理不当**:Reactor的流式操作可能会在某个阶段抛出异常,而这些异常如果没有被捕获,则会导致整个流程中断。 2. **异步执行特性**:Reactor是基于反应式编程模型的库,其方法通常是异步执行的。如果验证逻辑没有正确地与流式操作结合,可能导致条件判断未被执行。 3. **SignedJWT验证逻辑错误**:`SignedJWT.verify`方法可能抛出了异常,或者返回的结果未能正确传递到后续逻辑中。 以下是详细的解决方案及代码示例。 --- ### 解决方案 #### 1. 异常捕获与处理 确保在Reactor的流式操作中正确捕获异常。可以使用`onErrorResume`或`doOnError`来处理可能发生的异常[^1]。 ```java import reactor.core.publisher.Mono; public Mono<Boolean> verifyToken(String token) { return Mono.fromCallable(() -> { try { SignedJWT signedJWT = SignedJWT.parse(token); if (signedJWT.verify(new RSASSAVerifier(publicKey))) { return true; } else { return false; } } catch (Exception e) { // 捕获异常并记录日志 System.err.println("Token verification failed: " + e.getMessage()); throw new RuntimeException("Token verification failed", e); } }) .onErrorResume(e -> { // 处理异常情况 System.err.println("Error occurred during token verification: " + e.getMessage()); return Mono.just(false); }); } ``` --- #### 2. 确保验证逻辑正确 检查`SignedJWT.verify`方法调用是否正确。如果验证失败,应该明确返回结果,而不是依赖于隐式的`if`语句[^2]。 ```java import com.nimbusds.jose.JOSEException; import com.nimbusds.jose.proc.SecurityContext; import com.nimbusds.jwt.SignedJWT; import com.nimbusds.jose.proc.BadJOSEException; public boolean verifyToken(String token) throws JOSEException, BadJOSEException { SignedJWT signedJWT = SignedJWT.parse(token); return signedJWT.verify(new RSASSAVerifier(publicKey)); } ``` 在上述代码中,`verifyToken`方法会直接返回验证结果,避免了因异常导致流程中断的问题。 --- #### 3. Reactor流式操作中的条件判断 如果需要在Reactor的流式操作中进行条件判断,可以使用`filter`或`flatMap`来实现[^3]。 ```java import reactor.core.publisher.Flux; public Flux<String> processTokens(List<String> tokens) { return Flux.fromIterable(tokens) .flatMap(token -> { try { SignedJWT signedJWT = SignedJWT.parse(token); if (signedJWT.verify(new RSASSAVerifier(publicKey))) { return Mono.just(token); } else { return Mono.empty(); } } catch (Exception e) { System.err.println("Token verification failed: " + e.getMessage()); return Mono.empty(); } }) .onErrorResume(e -> { System.err.println("Error occurred during token processing: " + e.getMessage()); return Flux.empty(); }); } ``` --- ### 注意事项 - 确保`publicKey`对象已正确初始化,并且与签名时使用的私钥匹配。 - 如果项目中存在多个版本的`reactor-core`或`nimbus-jose-jwt`库,可能会导致方法冲突问题。请检查项目的依赖树,确保版本一致[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值