3

Is there any way to assert in a method that input String has certain length?

I tried assert stringName[4]; but seems like it doesn't work

Sinto
  • 3,915
  • 11
  • 36
  • 70
Miervatea
  • 53
  • 1
  • 2
  • 5
  • 2
    `assert stringName.length() == 4;` perhaps? (That's assuming you're using Java's built-in assertions.) – Jon Skeet Aug 28 '18 at 05:39
  • Please share your test case and the piece of code you are trying to test – codeLover Aug 28 '18 at 05:39
  • Possible duplicate [What does the “assert” keyword do?](https://stackoverflow.com/questions/3018683/what-does-the-assert-keyword-do) – AxelH Aug 28 '18 at 05:59

4 Answers4

7

If you just want to use the Java's assert keyword and not any library like JUnit, then you can probably use:

String myStr = "hello";
assert myStr.length() == 5 : "String length is incorrect";

From the official docs:

The assertion statement has two forms. The first, simpler form is:

assert Expression1;

where Expression1 is a boolean expression. When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.

The second form of the assertion statement is:

assert Expression1 : Expression2 ;

where: Expression1 is a boolean expression. Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)

You can use the following if you're using a testing library like JUnit:

String myStr = "hello";
assertEquals(5, myStr.length());

Update: As correctly pointed out in the comments by AxelH, after compilation, you run the first solution as java -ea AssertionTest. The -ea flag enables assertions.

AxelH
  • 14,325
  • 2
  • 25
  • 55
ayushgp
  • 4,891
  • 8
  • 40
  • 75
  • 1
    @AxelH I wouldn't let any assert statement run in a production environment, either. – Jai Aug 28 '18 at 05:53
  • so if the length is not 5, it throws you message saying "String length is incorrect"? I tried this but doesn't show up in console - can you possibly think why it's not showing up? – Miervatea Sep 06 '18 at 04:23
  • @Miervatea did you put in the `-ea ` when running as I've mentioned at the end of the answer? – ayushgp Sep 06 '18 at 05:44
  • public class practice{ public static void main(String args[]) { String s1 = "hello"; assert s1.length() == 5 : "incorrect length of String"; }} it's terminating itself before I type -ea thing - sorry it's very dumb : / – Miervatea Sep 07 '18 at 03:54
  • You don't need to enter -ea after running the program. If you're using an IDE like intellij, this answer: https://stackoverflow.com/questions/18168257/where-to-add-compiler-options-like-ea-in-intellij-idea can probably help you with entering the flag. If you're running the program from a terminal/cmd prompt, instead of running `java Practice` run `java -ea Practice`. – ayushgp Sep 07 '18 at 06:22
2

Use AssertJ hasSize():

assertThat(stringName).hasSize(4)
hertzsprung
  • 9,445
  • 4
  • 42
  • 77
0

Instead of using assert, I'd recommend using Exception to check the state of the variables as a simple demo:

String[] arr = new String[3];
if (arr.length != 4) {
    throw new IllegalStateException("Array length is not expected");
}

This will directly give the hint by exceptions and you don't need to bother with assert in jvm options.

Exception in thread "main" java.lang.IllegalStateException: Array length is not expected
    at basic.AssertListLength.main(AssertListLength.java:7)
Hearen
  • 7,420
  • 4
  • 53
  • 63
0

If you are using hamcrest, then you can do:

 assertThat("text", hasLength(4))

See http://hamcrest.org/JavaHamcrest/javadoc/2.2/ > CharSequenceLength

Good thing about this is that it will have a proper error message, among others including the string itself.

Gabriel Furstenheim
  • 2,969
  • 30
  • 27