Zuul没有抛出预期的异常

时间:2018-07-31 18:01:19

标签: 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

当我通过邮递员访问服务时,我看到消息中抛出了正确的异常

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我补充说

"Access-Control-Allow-Headers"

工作。因此,我摆脱了@Before public void setUp() { CounterFactory.initialize(new EmptyCounterFactory()); }