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

  • SEARCH
  • Home
  • 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 7182565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:47:49+00:00 2026-05-28T17:47:49+00:00

I am testing a function that permit to connect to FTP server. Here is

  • 0

I am testing a function that permit to connect to FTP server.

Here is one of my test who works properly:

@Test
public void connectTestValid()
{
    assetSource.setPassword("password");
    assetSource.setUsername("user");
    assetSource.setServerAddress("127.0.0.1");
    assetSource.setServerPort(21);
    connectionSuccess = false;

    connectionSuccess = ftpFolderTest.connectFTP(ftpClient);
    if (!connectionSuccess)
    {
        fail("Expected Connection success");
    }
}

I want to test if the connectFTP() method throw an exception when the serverAddress is invalid.

Here is my test:

@Test(expected = Exception.class)
public void connectTestInvalidServerAddress()
{
    assetSource.setPassword("password");
    assetSource.setUsername("user");
    assetSource.setServerAddress("1");
    assetSource.setServerPort(21);
    connectionSuccess = false;

    connectionSuccess = ftpFolderTest.connectFTP(ftpClient);
}

Here is my function:

protected boolean connectFTP(FTPClient ftp)
{
    try
    {

        ftp.connect(getAssetSource().getServerAddress());

        if (!ftp.login(getAssetSource().getUsername(), getAssetSource().getPassword()))
        {
            logger.error("Login Failed");
            ftp.disconnect();
            return connectionSuccess = false;
        }// if

        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))
        {
            logger.error("Connection Failed");
            ftp.disconnect();
            return connectionSuccess = false;
        }// if
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return connectionSuccess = false;
    }
    return connectionSuccess = true;
}

Presently, the test doesn’t work.
Thanks for your help!

  • 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-05-28T17:47:51+00:00Added an answer on May 28, 2026 at 5:47 pm

    The reason the test is not passing is that it is expecting an Exception to be thrown, but the exception is being caught inside the ‘connectFTP’ method, which is then returning false.

    Whether you return false when connection fails or throw an exception depends on the semantics of your code. Based on the boolean return value, it seems like you’re expecting false to be returned when there is an exception. In that case you’ll want to do

    org.junit.Assert.assertFalse(connectionSuccess); 
    

    instead of using (expected = Exception.class) in the @Test annotation.

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

Sidebar

Related Questions

This is a function that I want to write a unit test for: public
Im trying to use function that i put in remote server Now in testing
I'm testing a function that obtains a skeleton object from one helper object, modifies
I'm testing the VB function below that I got from a Google search. I
I am testing a function called extractions that operates over any list. extractions ::
I am working with a unit-testing suite that hijacks function calls and tests expected
I want to ask if I should include the function that I am testing
I have a PHP/jQuery scraper function that essentially looks for web page content. One
I have a python program with a global function that is painful to test
Quick one for you guys. Say I have a function that outputs a string:

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.