Repository: assertj/assertj

Issue #1441: UnknownFormatConversionException when asserting String with `%` sign
Created: 2019-02-18T22:16:57Z
Summary
I have a test written in Kotlin. It throws UnknownFormatConversionException when I try to assert a String containing % character. The same test in Java works properly.

A hint: Java test calls method org.assertj.core.api.AbstractAssert#isEqualTo, while Kotlin test calls org.assertj.core.api.AbstractStringAssert#isEqualTo (which is buggie when there is no args).

Example

Test in Java - works:

import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;

public class AssertJTest {
  @Test
  public void assertJ12() {
    assertThat("foo %").isEqualTo("foo %");
  }
}

The same test in Kotlin - fails:

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class AssertJTest {
  @Test
  fun assertJ12() {
    assertThat("foo %").isEqualTo("foo %")
  }
}
Failure message is:
java.util.UnknownFormatConversionException: Conversion = '%'
	at java.base/java.util.Formatter.checkText(Formatter.java:2732)
	at java.base/java.util.Formatter.parse(Formatter.java:2718)
	at java.base/java.util.Formatter.format(Formatter.java:2655)
	at java.base/java.util.Formatter.format(Formatter.java:2609)
	at java.base/java.lang.String.format(String.java:2897)
	at org.assertj.core.api.AbstractStringAssert.isEqualTo(AbstractStringAssert.java:310)
	at controllers.api.AssertJTest.assertJ12(AssertJTest.kt:9)

Duplicate issue #1500: Bug: The commit for adding "AbstractStringAssert.isEqualTo(String string, Objects... param)" breaks existing unit tests
Created: 2019-05-08T19:23:50Z
Summary
The following commit that added the AbstractStringAssert.isEqualTo(String string, Objects... param) method to AbstractStringAssert.java breaks existing unit tests that have "invalid format characters" in the string because it overrides the base class method and assumes that the parameter is now a formatted string: 37eacd1
Note: I was using 3.11.1 and just upgraded to 3.12.2.
Example
This is an example test (in Kotlin) that now fails:
val encodedString = "foo bar+biz&baz*qux~qix/qax".urlEncode()
assertThat(encodedString).isEqualTo("foo%20bar%2Bbiz%26baz%2Aqux~qix%2Fqax")

with an exception like this:
java.util.UnknownFormatConversionException: Conversion = 'F'
	at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2691)
	at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2720)
	at java.util.Formatter.parse(Formatter.java:2560)
	at java.util.Formatter.format(Formatter.java:2501)
	at java.util.Formatter.format(Formatter.java:2455)
	at java.lang.String.format(String.java:2940)
	at org.assertj.core.api.AbstractStringAssert.isEqualTo(AbstractStringAssert.java:312)
	at com.example.test.urlEncode_Always_ReturnsEncodedString(StringExtensionsTest.kt:43)

The current workaround is to cast the string to Any so that AbstractAssert.isEqualTo(Object expected) is the method that is actually invoked:
val encodedString = "foo bar+biz&baz*qux~qix/qax".urlEncode()
assertThat(encodedString).isEqualTo("foo%20bar%2Bbiz%26baz%2Aqux~qix%2Fqax" as Any)

Closing comment (2019-05-08T19:58:54Z): This one seems to be a duplicate of #1441 and #1440, isn't it?
Good news, it is already fixed on master (d28c346) and will be part of 3.13.0.