Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:40:54+00:00 2026-06-10T00:40:54+00:00

In my tests I’m using a connection to a DB, I open the connection

  • 0

In my tests I’m using a connection to a DB, I open the connection in @Before
and close it in @After.

My question is what happens if an exception was thrown during the test?

How would I be able to close the connection?

I’m looking for something that is equivalent to finally that comes after try and catch block.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:40:55+00:00Added an answer on June 10, 2026 at 12:40 am

    @After will do what you want. According to its documentation:

    All @After methods are guaranteed to run even if a Before or Test method throws an exception.

    (This intuitively makes sense to me, since I imagine the test method as wrapped in a try block that will catch any exception and convert it to a test failure. Hence the test fails, then the teardown block runs.)

    Note that it doesn’t specify that the method will run if an Error is thrown. Usually though you’d only use the @After method to maintain the test state, and this won’t present a problem because no more tests would run anyway in this case. If you do use any resources in the test that absolutely have to be cleaned up (e.g. native hooks), then it’s best to do this in a try-finally block within the test method itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running my tests for a rails application using test/unit and capybara. I
I am writing tests to test Infopath Forms to open in Form Control, my
jQuery tests the validity of a date via: !/Invalid|NaN/.test(new Date(value)) new Date(dateString) is the
I execute some tests within my application, but before doing that I have to
After running my automated tests in Visual Studio 2010, the Code Coverage Results tab
Classical question at tests: I have 4 dimensions: Markets: Sparse, 10 stored members, 10
I have some unit tests I've written to test my Django application. One test
I've ran some tests with sockets (using fsockopen() and stream_socket_client() ) and cURL to
For tests of some small JBoss enterprise apps I would like to use JUnit,
I need run all tests from my multimodule project using IDE. Like I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.