是否可以从junit测试的主要方法中获取变量? [英] Is it possible to obtain variables in main method from junit test?

查看:257
本文介绍了是否可以从junit测试的主要方法中获取变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Junit测试中,是否可以在main方法中测试变量?

In a Junit test, is it possible to test for variables in the main method?

我的主要方法如下:

package edu.blah.class.project1;

public class Program {

public static void main(String[] args) {
    try {
        int result = Utility.analyze();
    } catch (Exception e) {
        System.out.println("Error");
    }
    }

}

是否可以在JUnit类中获取变量结果?是使其成为公共变量的唯一方法吗?谢谢!

Is it possible to obtain the variable result in a JUnit class? Is the only way to make it a public variable? Thanks!

推荐答案

在Junit测试中,是否可以在main方法中测试变量?

In a Junit test, is it possible to test for variables in the main method?

Err ...不.除非您有一个JUnit测试明确地调用main方法,否则main方法将不会在单元测试中调用.

Err ... no. Unless you have a JUnit test that calls the main method explicitly, then the main method won't be called in a unit test.

此外,这里的变量是result,它是一个局部变量,无法进入测试某些局部变量的方法. (Java根本不允许它...)

Besides, the variable here is result which is a local variable, and there is no way to reach into a method to test some local variable. (Java simply doesn't allow it ...)

是否可以在JUnit类中获取变量结果?

Is it possible to obtain the variable result in a JUnit class?

在此示例中,result获取调用Utility.analyze()的值.没有理由为什么JUnit测试也不能这样做……从而获得相同的值. (或者至少在此示例中可以.)

In this example, result gets the value of the call Utility.analyze(). There is no reason why a JUnit test could not do that too ... and thereby get the same value. (Or at least it could in this example.)

是使其成为公共变量的唯一方法吗?

Is the only way to make it a public variable?

不...见上文.

但是,如果您实际上是在主方法的上下文中尝试测试result 的值,那么您是对的.保留该值的唯一方法是将其公开为static变量.虽然不一定必须是public static. (实际上,如果您准备写一些讨厌的"反射,您甚至可以让JUnit测试来提取并测试private static字段的值.)

However, if you are actually trying to test the value of result in the context of the main method, then you are right. The only way to get hold of the value would be to expose it as a static variable. It doesn't necessarily have to be public static though. (Indeed, if you were prepared to write do some "nasty" reflection you could even get your JUnit test to extract and test the value of a private static field.)

但是,我认为您采用的是错误的方法.与其尝试弄清楚如何使用静态方法(或任何方法),不如重新组织代码以使其更具可测试性,这会更好.例如:

However, I think you are taking the wrong approach top this. Rather than trying to figure out how to reach into a static method (or any method), it would be better if you restructured the code to make it more testable. For example:

package edu.blah.class.project1;

public class Program {

    public int run(String args[]) {
        return Utility.analyze();
    }

    public static void main(String[] args) {
        try {
            new Program().run(args);
        } catch (Exception e) {
            System.out.println("Error");
        }
    }
}

通过少量的重组(像这样),您可以使编写所有重要功能的单元测试变得更加容易.您还剩下问题",仍然很难对main方法进行单元测试.但是,您已经将该方法简化为可以通过肉眼检查验证其正确性的程度:现在无需进行单元测试main.

With a small amount of restructuring (like this), you can make it a lot easier to write unit tests for all of the important functionality. You are left with the "problem" it is still hard to unit test the main method. However, you have reduced the method to the point that it is so simple that you can verify it is correct by visual inspection: unit testing main is now unnecessary.

这篇关于是否可以从junit测试的主要方法中获取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆