You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shenyu.apache.org by Kunshuai Zhu <jo...@gmail.com> on 2021/07/31 11:59:00 UTC

How to deal with `JwtUtils.getUserInfo()` when writing unit tests?

Hi friends,
I ran into trouble while writing unit tests. You can see
`PermissionServiceImpl#getPermissionMenu`.
The code in 74th line is `UserInfo userInfo = JwtUtils.getUserInfo();`
Here comes the problem that it will throw NullPointerException. So
that the following lines can't be test.
What should i do? Can I import "powermock" to mock `JwtUtils.getUserInfo()`?

Re: How to deal with `JwtUtils.getUserInfo()` when writing unit tests?

Posted by Kunshuai Zhu <jo...@gmail.com>.
Wow~ Thanks, that helped me a lot.

Liming Deng <de...@apache.org> 于2021年8月1日周日 上午10:42写道:
>
> Hi~ kunshuai,
>
> You can also use mockito, There is a sample here
> https://github.com/apache/incubator-shenyu/blob/38b63d28b245dbd4070f054baff056091020f171/shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/test/java/org/apache/shenyu/client/springcloud/init/SpringCloudClientBeanPostProcessorTest.java#L84
>
> Kunshuai Zhu <jo...@gmail.com> 于2021年7月31日周六 下午7:59写道:
>
> > Hi friends,
> > I ran into trouble while writing unit tests. You can see
> > `PermissionServiceImpl#getPermissionMenu`.
> > The code in 74th line is `UserInfo userInfo = JwtUtils.getUserInfo();`
> > Here comes the problem that it will throw NullPointerException. So
> > that the following lines can't be test.
> > What should i do? Can I import "powermock" to mock
> > `JwtUtils.getUserInfo()`?
> >

Re: How to deal with `JwtUtils.getUserInfo()` when writing unit tests?

Posted by Liming Deng <de...@apache.org>.
Hi~ kunshuai,

You can also use mockito, There is a sample here
https://github.com/apache/incubator-shenyu/blob/38b63d28b245dbd4070f054baff056091020f171/shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/test/java/org/apache/shenyu/client/springcloud/init/SpringCloudClientBeanPostProcessorTest.java#L84

Kunshuai Zhu <jo...@gmail.com> 于2021年7月31日周六 下午7:59写道:

> Hi friends,
> I ran into trouble while writing unit tests. You can see
> `PermissionServiceImpl#getPermissionMenu`.
> The code in 74th line is `UserInfo userInfo = JwtUtils.getUserInfo();`
> Here comes the problem that it will throw NullPointerException. So
> that the following lines can't be test.
> What should i do? Can I import "powermock" to mock
> `JwtUtils.getUserInfo()`?
>