多次投掷多次测试

时间:2014-03-03 14:24:11

标签: java testing throw

我想创建一个方法来测试不同的东西,根据问题抛出错误(然后退出程序)。 我对抛出异常并不熟悉......这种方法是编程的好方法吗?

private static void testConnexions() throws IOException, Exception {

    File file = null;
    Socket socket;

    try {
        // Test access to the repository:
        file = new File(pdfRepository);
        if (!file.canRead())
            throw new SecurityException();
        if (!file.canWrite())
            throw new SecurityException();
        if (!file.exists())
            throw new SecurityException();
        if (!file.isDirectory())
            throw new SecurityException();

        // Connection to  Filenet:      
        connexion = new FilenetConnexion(filenetURI, objectStoreName,
                stanza, dossierRacine, userName, password);
        connexion.connect();

        // Connection to a socket:
        socket = new Socket(serveurCV, portCV);

        // Initialize the JavaMail Session:
        Properties props = System.getProperties();
        if (serveurSMTP != null)
            props.put("mail.smtp.host", serveurSMTP);
        Session session = Session.getInstance(props, null);

} catch (SecurityException e) {
    e.getMessage();
    e.printStackTrace();
} catch (UnknownHostException e) {
    e.getMessage();
    e.printStackTrace();
} catch (IOException e) {
    e.getMessage();
    e.printStackTrace();
} catch (Exception e) {
    e.getMessage();
    e.printStackTrace();
} 
}

我想抓住一条足够详细的消息,知道是否无法写入存储库,或者System.getProperties()是否出现错误等等。

提前感谢您的帮助!

修改 这是我在所有贡献中选择的解决方案,希望它可以帮助某人:

private static void testConnexions() {

        File file = null;
        Socket socket;

        // Test access to the repository:
        try {
            file = new File(pdfRepository);
            if (!file.canRead())
                throw new SecurityException(pdfRepository + " can't be read.");
            if (!file.canWrite())
                throw new SecurityException(pdfRepository + " can't be written.");
            if (!file.exists())
                throw new SecurityException(pdfRepository + " doesn't exist.");
            if (!file.isDirectory())
                throw new SecurityException(pdfRepository + " isn't a folder.");    
        } catch (SecurityException e) {
            logger.error(e.getMessage());
            System.exit(1);
        }

        // Connection to  FileNet       
        try {
            connexion = new FilenetConnexion(filenetURI, objectStoreName,
                    stanza, dossierRacine, userName, password);
            connexion.connect();
        } catch (Exception e) {
            logger.error("Impossible to connect to FileNet. " + e.getMessage());
            System.exit(2);
        }

        // Connection to FrontalSocket
        try {
            socket = new Socket(serveurCV, portCV);
        } catch (UnknownHostException e) {
            logger.error("Impossible to connect to FrontalSocket. " + e.getMessage());
            System.exit(3);
        } catch (IOException e) {
            logger.error("Impossible to connect to FrontalSocket. " + e.getMessage());
            System.exit(3);
        }

        // Initialize the JavaMail session
        try {
            Properties props = System.getProperties();
            if (serveurSMTP != null)
                props.put("mail.smtp.host", serveurSMTP);
            else{
                logger.error("The SMTP host name is null");
                System.exit(4);
            }
            Session session = Session.getInstance(props, null);
        } catch (Exception e) {
            logger.error("Impossible to connect to SMTP server. " + e.getMessage());
            System.exit(4);
        }
    }

2 个答案:

答案 0 :(得分:3)

您可以采取以下几种方式,选择最适合您情景的方式:

  • 根据每个错误情况抛出不同的异常。很容易将Exception子类化并以这种方式创建区别。
  • 根据错误情况,使用特定错误消息抛出相同的异常。

案例1的一个例子:

首先定义自己的例外:

public class CannotReadException extends Exception {
   // This is a separate class in your project
}
public class CannotWriteException extends Exception {
   // This is a separate class in your project
}

然后扔掉并抓住它们:

try {
        // Test access to the repository:
        file = new File(pdfRepository);
        if (!file.canRead())
            throw new CannotReadException();
        if (!file.canWrite())
            throw new CannotWriteException();
        ...
} catch (CannotReadException e) {
   // Do stuff for the specific error
} catch (CannotWriteException e) {
   // Do stuff for the specific error
}

或,案例2:

try {
        // Test access to the repository:
        file = new File(pdfRepository);
        if (!file.canRead())
            throw new SecurityException( "cannot read" );
        if (!file.canWrite())
            throw new SecurityException( "cannot write" );
        ...
} catch (SecurityException e) {
   // Get to your specific message using e.getMessage();
}

答案 1 :(得分:1)

在这种情况下,我可以建议抛出用户定义异常并传递足够详细的消息以了解对该错误负责。

public class MyException extends Exception {
    //override the needed methods.
}

然后使用已删除的消息抛出您自己定义的异常。

try {
    // Test access to the repository:
    file = new File(pdfRepository);
    if (!file.canRead())
        throw new MyException("The fine has no read permission");