java - Junit 和 java 类。

标签 java design-patterns junit

下面是一个简单的java类文件,用于检查用户提供的文件是否在主目录下。当文件不在主目录下时抛出异常。

public class A {
  public static void main(String args[]) {
     if (new A().processArgs(args[0]) {
        throw Exception("Not under home directory");
     }
  }

  // A simple method to check if the file is at home directory
  private boolean processArgs(String s) {
    File f = new File(s);
    String userHome = System.getProperty("user.home");
    if (s.startsWith(userHome) && f.exists() && additionalLogic())
      return true;  
    else
      return false;
  }
  // Additional business Logic
  private boolean additionalBusinessLogic() {
    // Do wonderful things.
  }
}

我想写一个简单的 Junit 测试用例来测试 java 类。测试的主要关注点是附加的业务逻辑方法。有没有一种方法可以绕过检查目录必须在用户主目录下的位置。

我不愿意在我的主类中添加逻辑以使其了解 Junit 类。有更好的方法吗?

最佳答案

虽然 fab 的解决方案没有任何问题,但我还是决定再写一个:

public class Main {
    public static void main(String args[]) {
        // TODO: Should check args length
        Validator validator = new Validator();
        validator.validateArgs(args[0]);
    }
}

public interface Configuration {
    public String getHomeDirectory();
}

public class DefaultConfiguration implements Configuration {
    public String getHomeDirectory() {
        String home = System.getProperty("user.home");
        if (home == null) {
            throw new RuntimeException("User home directory is not set!");
        }
        return home;
    }
}

public class Validator {
  private Configuration configuration;

  public Validator() {
     this(new DefaultConfiguration());
  }

  public Validator(Configuration configuration) {
     this.configuration = configuration;
  }

  // A simple method to check if the file is at home directory
  public void validateArgs(String s) {
    File f = new File(s);
    if (!s.startsWith(configuration.getHomeDirectory()) || !f.exists() || !additionalBusinessLogic())
      throw new RuntimeException("Not under home directory!");
  }

  // Additional business Logic
  private boolean additionalBusinessLogic() {
     // TODO...
     return true;
  }
}

public class ValidatorTest {
  @Test
  public void validateValidArgsTest() {
     final String homeDirectory = ".."; // TODO
     String existingFile = homeDirectory + ".."; // TODO
     new Validator(new Configuration() {
       public String getHomeDirectory() {
          return homeDirectory;
       }
     }).validateArgs(existingFile);
  }

  @Test(expected = RuntimeException.class)
  public void validateInvalidArgsTest() {
     String existingFile = ".."; // TODO
     new Validator(new Configuration() {
       public String getHomeDirectory() {
          return "-INVALID PATH-";
       }
     }).validateArgs(existingFile);
  }
}

关于java - Junit 和 java 类。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863289/

相关文章:

java - 生产者和消费者共享队列,其值来自 HttpRequest

java - 在一个父类(super class)集合中的不同对象(扩展公共(public)父类(super class))上循环

java - 如何用mockito模拟静态方法进行单元测试

java - 正则表达式不会从字符串中删除字符

java - 方法返回类型 Object 中的 NullPointerException

c++ - 接口(interface),在 C++ 中隐藏具体的实现细节

mysql - 保持多个表的评级

junit - 如何在 Cypress.io 的 JUnit 报告中测试失败后附加屏幕截图

Java 单元测试返回参数对象

java - 如何添加到基于 Java 中特定谓词的集合?