unit-testing - Zuul 没有抛出预期的异常

标签 unit-testing testing junit netflix-zuul zuul-testing

我正在抛出一个 ZuulRuntimeException 并编写了一个测试用例来验证它,但它抛出了一个不同的异常。

代码:

public String extractCdsid(String accessToken) {

        ObjectMapper objectMapper = new ObjectMapper();
        String cdsid = "";

        try {

            String payload = JwtHelper.decode(accessToken).getClaims();
            cdsid = objectMapper.readTree(payload).get("upn").textValue();

        } catch (NullPointerException e) {      
            ZuulException ex = new ZuulException(
                    "Token does not contain upn. Please send a valid that contains upn(cdsid)", 401, "");

            throw new ZuulRuntimeException(ex);     

        } }

测试用例:

@Test(expected = ZuulRuntimeException.class)
    public void getPrincipalHeader_withAuthorizationHeaderNotContainUpn_returnsException(){

        // mock
        MockHttpServletRequest req = new MockHttpServletRequest();
        req.addHeader("Authorization", "sendingtokenthatdoesnotcontainupn");

        // run
        tokenParser.getPrincipalHeader(req);        

    }

我得到了

java.lang.Exception: Unexpected exception, expected<com.netflix.zuul.exception.ZuulException> but was<java.lang.IllegalStateException>
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: CounterFactory not initialized
    at com.netflix.zuul.monitoring.CounterFactory.instance(CounterFactory.java:42)
    at com.netflix.zuul.exception.ZuulException.incrementCounter(ZuulException.java:73)
    at com.netflix.zuul.exception.ZuulException.<init>(ZuulException.java:54)
    at com.cv.vadr.vadrgateway.util.TokenParser.extractCdsid(TokenParser.java:51)
    at com.cv.vadr.vadrgateway.util.TokenParser.getPrincipalHeader(TokenParser.java:32)
    at com.cv.vadr.vadrgateway.util.TokenParserTest.getPrincipalHeader_withAuthorizationHeaderNotContainUpn_returnsException(TokenParserTest.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    ... 16 more

当我通过 postman 访问服务时,我看到正确的异常被抛出

最佳答案

我遇到了同样的问题,对我来说

@Before
public void setUp() {
   CounterFactory.initialize(new EmptyCounterFactory());
}

成功了。多亏了我摆脱了 java.lang.IllegalStateException: CounterFactory not initialized

关于unit-testing - Zuul 没有抛出预期的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51619444/

相关文章:

asp.net-mvc - 集成测试 ASP.NET MVC 应用程序

unit-testing - 如何在 Go 中模拟包方法?

java - 当运行程序调用 '@Before' 方法时,Spring 不会 Autowiring 字段

java - JUnit - 在 @BeforeAll 之前执行的方法和在 @AfterAll 之后执行的方法

javascript - 如何在 JavaScript 单元测试中模拟 localStorage?

java - 使用假 smtp 服务器生成 MailSendException

unit-testing - 模拟和监视键盘事件,并在 native 中开 Jest

css - 跨浏览器测试 : is there a way to run and control multiple browsers at the same time?

java - 使用 AND 运算符 CriteriaBuilder 的规范和谓词集合

python - 如何使用 Python 测试 API 客户端?