English 中文(简体)
测试课堂本身是否好?
原标题:Is it a good idea to test classes in its own main?

When it comes to test your code, is it a good idea to use its own main for testing? The other option would be to use an external file with this tests.

例如:

Class Foo{
    Foo(){/*...*/}
    public boolean met1(){/*...*/}
    //other metodes...

    //Tests
    public static void main(String args[]){
         Foo f = new Foo();
         //Test the correctness of data...

         boolean r = f.met1();
         //check the result...
    }
}

What are the pros and cons of this technic. Maybe there are better approaches for testing code that i did not mention.

最佳回答

如果测试能够真正做到<>自动,那么你就应当使用外部档案,如Junnit等。 支持进行所有测试、持续整合、与冰绿线和红线报告等渠道......

如果是全球倡议的一个要素,你想要能够提出和看到它所看的是什么,或许可以人工点点,看什么情况,那么它就使用主机。 评注。

同样,如果方案管理员可能希望测试和观察变幻影中正在发生的情况,那么指挥线会变幻等等。 它使用主机。 评注。

问题回答

It is OK if you want just to run your code when it was completed to check the result. But I prefer special Main class and call these method from it (just not to have junk code in many classes). But anyway JUnit tests are much better.

确实如此之多,但使用同一类别内的主要方法有好处,即你可以在主要方法内使用该类的私人静态类别。

例如:

public class Foo
{

    private static String someText = "blah blah";

    public Foo(){/*...*/}

    public static void main(String args[])
    {
         System.out.println(someText);
    }
}

我认为,除了使你的班级比需要大外,还有别的。 然而,我认为,应当努力起草正式测试,作为有助于避免错误的框架的一部分。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签