简体   繁体   中英

Junit AssertionError

I get an Assertion Error while running a JUnit test in my Java Gradle Project.

In this test I'm testing the following method:

    @Override
public Projectile shoot(Point initialPosition, int angle, Player player)
{
    if (!onCoolDown)
    {
        if (Config.soundfx)
        {
            sound = Gdx.audio.newSound(Gdx.files.internal("soundEffects/shoot.mp3"));

            Random r = new Random();
            double randomValue = 1.2 + (1.4 - 1.2) * r.nextDouble();
            sound.setPitch(sound.play(1.0f), (float)randomValue);
            //sound.play(1.0f);
        }
        shotsRemaining--;
        onCoolDown = true;
        cooldownTimer.schedule(new TimerTask() {
            @Override
            public void run()
            {
                onCoolDown = false;
            }
        }, cooldown);
        Point aimPoint = new Point();
        aimPoint.x = initialPosition.x + (int) (Math.sin(Math.toRadians(angle)) * 30);
        aimPoint.y = initialPosition.y + (int) (Math.cos(Math.toRadians(angle)) * 30);
        return new Projectile(angle, 10, ammunition, player, aimPoint, 0);
    }
    return null;
}

and this is the JUnitTest:

    @Test
public void testShoot_posY_posX_posAngle() {
    System.out.println("shoot");

    Point initialPosition = new Point(10, 10);
    int angle = 10;
    Player player = new Player("testPlayer", "");
    HandGun instance = new HandGun("testHandGun", 0, new CircleCharacter("testCharacter", 0, 0, initialPosition));
    Projectile expResult = new Projectile(angle, 10, instance.getAmmunition(), player, initialPosition, 0);
    Projectile result = instance.shoot(initialPosition, angle, player);
    assertEquals("projectile: " + result.toString() + " does not equal expected projectile: " + expResult.toString(), expResult, result);
}

and this is the error:

java.lang.AssertionError: projectile:  does not equal expected projectile:  expected:<ammunition.Projectile@749f698d> but was:<ammunition.Projectile@6430d554>
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 weapon.HandGunTest.testShoot_posY_posX_posAngle(HandGunTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

the entire test works except the assertEquals. As soon as it tries to execute the assertEquals it throws the exception.

Have you overriden hashcode() and equals() and toString() method for projectile ?

however, The Assertion error is because the expResult and result are not equal

There are couple of things you should correct-

  • As pointed in other answers, you should override hashCode() and equals() method in your Projectile class.
  • In your unit test, you should then pass the objects to the assertEquals(), and not use the toString() for comparison purpose.
  • Override toString() ONLY to make your object readable.

override the toString method of your class Projectile. Change your equals to

 assertEquals("projectile: " + result.toString() + " does not equal expected projectile: " + expResult.toString(), expResult.toString(), result.toString());

Right now you are testing if the instances are equal

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