Skip to content

Removed exception handling (test smell)#567

Merged
brian-brazil merged 1 commit intoprometheus:masterfrom
eas5:test_improvement
Aug 13, 2020
Merged

Removed exception handling (test smell)#567
brian-brazil merged 1 commit intoprometheus:masterfrom
eas5:test_improvement

Conversation

@eas5
Copy link
Copy Markdown
Contributor

@eas5 eas5 commented Aug 13, 2020

This is a test refactoring.

Problem:
The Exception Handling test smell occurs when explicitly a passing or failing of a test method is dependent on the production method throwing an exception.

Solution:
The use of JUnit's exception handling to automatically pass/fail the test instead of writing custom exception handling code or throwing an exception is encouraged.

Result:
Before:

  @Test
  public void testUnbound() throws IOException {
    CollectorRegistry registry = new CollectorRegistry();
    try {
      HTTPServer s = new HTTPServer(HttpServer.create(), registry, true);
      s.stop();
      fail("Should refuse to use an unbound HttpServer");
    } catch (IllegalArgumentException expected) {
    }
  }

After:

  @Test(expected = IllegalArgumentException.class)
  public void testRefuseUsingUnbound() throws IOException {
    CollectorRegistry registry = new CollectorRegistry();
    HTTPServer s = new HTTPServer(HttpServer.create(), registry, true);
    s.stop();
  }

@brian-brazil
Copy link
Copy Markdown
Contributor

Can you add the DCO?

Signed-off-by: Elvys Soares <eas5@cin.ufpe.br>
@eas5 eas5 force-pushed the test_improvement branch from 3b6ec5f to cfe595d Compare August 13, 2020 14:59
@eas5
Copy link
Copy Markdown
Contributor Author

eas5 commented Aug 13, 2020

@brian-brazil Sure! Done!

@brian-brazil brian-brazil merged commit c41bef4 into prometheus:master Aug 13, 2020
@brian-brazil
Copy link
Copy Markdown
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants