1

Using this piece of code:

@Test
public void testBallSize() {
    Ball b = new Ball(1f,1f,0.5f,0.5f, Color.WHITE);
    assertEquals("Color of the ball should be white.", Color.WHITE, b.getColor());

}

Produced:

   java.lang.AssertionError: Color of the ball should be white. expected: java.awt.Color<java.awt.Color[r=255,g=255,b=255]> but was: java.awt.Color<java.awt.Color[r=255,g=255,b=255]>
            at org.junit.Assert.fail(Assert.java:88)
            at org.junit.Assert.failNotEquals(Assert.java:834)
            at org.junit.Assert.assertEquals(Assert.java:118)
            at BallTest.testBallSize(BallTest.java:18)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
            at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
            at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
            at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
            at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

The colors appear to be the same. What am I doing wrong?

Idos
  • 15,053
  • 14
  • 60
  • 75
Testko
  • 31
  • 5
  • 3
    Maybe the alpha values of the two colors are different? According to the docs: `The result is true if and only if the argument is not null and is a Color object that has the same red, green, blue, and alpha values as this object.` – Daniel Zolnai Dec 05 '16 at 08:18
  • More information is required about your code. Please add more code. – ITguy Dec 05 '16 at 08:18
  • I think @DanielZolnai is right. https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#equals(java.lang.Object) – ACV Dec 05 '16 at 08:19
  • Great! The answer was in alpha value. For some reason, class picked default alpha value 127, and that's the reason why it wasn't showing true even though other color values were correct. Please, make it an answer and I will declare this q. closed. :-) @DanielZolnai – Testko Dec 05 '16 at 08:21
  • But still, this is interesting... why alphas were different? – ACV Dec 05 '16 at 08:56

1 Answers1

1

Maybe the alpha values of the two colors are different?

According to the docs:

The result is true if and only if the argument is not null and is a Color object that has the same red, green, blue, and alpha values as this object.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71