简体   繁体   English

JUnit测试可以从返回断言的对象中解析吗?

[英]Can a JUnit test resolve from a object returning a Assertion?

Can a JUnit test resolve from a object returning a Assertion? JUnit测试可以从返回断言的对象中解析吗?

For example, if I have a test that looks like this, would this work? 例如,如果我有一个看起来像这样的测试,这行得通吗?

@Test
public void testCase1() {
    TestObject to = new TestObject();
    to.login();
    to.runTest();
    // then assert success
    to.verifyTest();

}

public Class TestObject() {
  ....
  public Assert verifyTest() {
    return assertTrue("Test result not found.", this.validateTestResult() );
  }
}

Most Assert calls throw an AssertionError if they fail, so the code wouldn't look exactly the way you put it, but your code could be adjusted slightly and compile/run. 如果大多数Assert调用失败,则会引发AssertionError ,因此代码看起来与您的放置方式并不完全相同,但是可以对代码进行一些调整并进行编译/运行。 Because they are implemented as exceptions, you can call Assert methods from anywhere, including any helper classes you set up to help run your tests easier, as far deep in the stack as you'd like. 因为它们是作为异常实现的,所以您可以在任何地方调用Assert方法,包括为方便地运行测试而设置的任何帮助程序类,可以根据需要深入堆栈中。

EDIT : I do very much recommend setting up helper classes if you need to make a similar set of assertions against many objects. 编辑 :如果您需要对许多对象进行类似的断言设置,我非常建议您设置帮助器类。 I misunderstood and thought your TestObject was your system under test; 我误会了,以为您的TestObject是您的被测系统; the rest applies to that situation instead. 其余的适用于那种情况。

== ==

There's nothing to prevent you from calling Assert methods from within the class under test, but part of the intention for JUnit is to have clean test classes that are separate from your code classes. 没有什么可以阻止您从被测类中调用Assert方法的,但是JUnit的部分意图是使干净的测试类与您的代码类分开。 That way, the tests can evolve separately and often do not even need to change unless your class's interface changes. 这样,测试可以单独发展,并且除非类的接口发生更改,否则甚至根本不需要更改。 In my code, I put them in the same package in a separate "source folder", so you have: 在我的代码中,我将它们放在同一包中的单独的“源文件夹”中,因此您具有:

  • src/com/mypackage/project1/database/DatabaseAccessor.java src / com / mypackage / project1 / database / DatabaseAccessor.java
  • testsrc/com/mypackage/project1/database/DatabaseAccessorTest.java testsrc / com / mypackage / project1 / database / DatabaseAccessorTest.java
  • testsrc/com/mypackage/project1/database/DatabaseAccessorSystemTest.java testsrc / com / mypackage / project1 / database / DatabaseAccessorSystemTest.java

One of the reasons to separate things this way is to ensure that no testing code is ever run in production; 用这种方式分离事物的原因之一是确保生产中永远不会运行测试代码。 if the verifyTest method is in the same class, there's nothing to stop you from calling it within your class--or worse, from other classes calling it from elsewhere in your codebase. 如果verifyTest方法在同一类中,则没有什么可以阻止您在类中调用它-更糟的是,从其他类从代码库中的其他地方调用它。 You also avoid depending on junit.jar from production code. 您还避免从生产代码中依赖junit.jar。

If you are looking to make assertions in production code to avoid inconsistent state or illegal arguments, that's a different matter, and one for which you should avoid JUnit's Assert class--or, for that matter, assert statements (which are compiled out based on arguments to javac ). 如果您希望在生产代码中声明,以避免出现不一致的状态或非法参数,那是另一回事,对于它,您应该避免使用JUnit的Assert类-或为此,使用assert语句(根据javac参数)。 Instead, prefer a library like Guava's Preconditions . 而是喜欢像Guava的Preconditions这样的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM