Skip to content

Conversation

@gvma
Copy link
Contributor

@gvma gvma commented Apr 8, 2021

Problem:
The Exception Handling test smell occurs when the test outcome is manually determined through pass or fail method calls, dependent on the production method throwing an exception generally inside a try/catch block. This refactoring proposal aims to make the exception catching a responsibility of the test framework which is already provided by its API. Also, without using try/catch blocks, tests can be more straightforward and possibly more comprehensible and maintainable.

Solution:
We propose using JUnit's exception handling to automatically pass/fail the test instead of writing custom exception handling code. In this particular case, we added the possibly throwable object to test method signature and removed the redundant fail method call and message.

Result:
Before:
try { fooService.baz("fail"); fail("should not reach here"); } catch (IllegalMonitorStateException ex) { assertThat(cn.exceptionQps()).isZero(); }

After:
assertThrows(IllegalMonitorStateException.class, () -> { fooService.baz("fail"); }); assertThat(cn.exceptionQps()).isZero();

@CLAassistant
Copy link

CLAassistant commented Apr 8, 2021

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gvma
Copy link
Contributor Author

gvma commented Apr 8, 2021

Did my code broke CI pipeline?

@sczyh30 sczyh30 added the area/test Issue or PR related to test cases label Apr 9, 2021
Copy link
Member

@sczyh30 sczyh30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sczyh30
Copy link
Member

sczyh30 commented Apr 9, 2021

Did my code broke CI pipeline?

Maybe it's a flaky test:

[INFO] Running com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboConsumerFilterTest
[ERROR] Tests run: 6, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.132 s <<< FAILURE! - in com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboConsumerFilterTest
[ERROR] testDegradeSync(com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboConsumerFilterTest)  Time elapsed: 0.024 s  <<< FAILURE!
org.junit.ComparisonFailure: expected:<[fallback]> but was:<[normal]>
	at com.alibaba.csp.sentinel.adapter.dubbo.SentinelDubboConsumerFilterTest.testDegradeSync(SentinelDubboConsumerFilterTest.java:144)
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   SentinelDubboConsumerFilterTest.testDegradeSync:144 expected:<[fallback]> but was:<[normal]>
[INFO] 
[ERROR] Tests run: 22, Failures: 1, Errors: 0, Skipped: 0

@sczyh30 sczyh30 merged commit 00a560b into alibaba:master Apr 9, 2021
@sczyh30
Copy link
Member

sczyh30 commented Apr 9, 2021

Thanks for contributing!

hughpearse pushed a commit to hughpearse/Sentinel that referenced this pull request Jun 2, 2021
linkolen pushed a commit to shivagowda/Sentinel that referenced this pull request Aug 14, 2021
linkolen pushed a commit to shivagowda/Sentinel that referenced this pull request Aug 14, 2021
linkolen pushed a commit to shivagowda/Sentinel that referenced this pull request Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/test Issue or PR related to test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants