简体   繁体   中英

Can JUnit make assertions about all Strings?

Is there any way in JUnit (or any other testing framework) to make assertions about all possible inputs to a method?

Something like:

assertTrue(myClass.myMethod(anyString))

EDIT: I realize that there are an infinite number of possible inputs but I guess I was wondering if there were some frameworks that could statically analyze a method and detect that in all possible cases a certain result would be reached. Eg public static boolean myMethod(String input) { return true; } public static boolean myMethod(String input) { return true; } will always return true?

No, there is practically an unlimited number of possible inputs.
Its your job to separate them into test cases with (expected) equivalent behaviour. If such an artificial intelligence would exist, then it could also write the code to be tested.

There exist test case generators, that auto create test cases, but they are mostly useless. They produce a huge amount of test cases, and mainly only touch the code, instead of testing an expected result.

Such tools raise test coverage percentage, but in a very dubious way. (I would call that an illegal raise of test coverage: you should test, not touch!)

Such a tool is CodePro from Google. Use CodePro->Test Case generation (eg within Eclipse) On first you will be a bit suprised, it's not to bad to try it out. Then you will know the limits of auto test case generation.

You cannot do this with JUnit. The only way I think you could do such a thing would be using Formal Logic Verification

As said before, it's not possible. However, there's the approach of automated property based testing. This comes somehow as close as possible to your idea. Well, still far tough...

For instance, hava a look at scalacheck :

ScalaCheck is a library written in Scala and used for automated property-based testing of Scala or Java programs. ScalaCheck was originally inspired by the Haskell library QuickCheck, but has also ventured into its own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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