Junit AssertionError

时间:2015-11-03 12:16:39

标签: java gradle junit

在我的Java Gradle项目中运行JUnit测试时出现断言错误。

在这个测试中,我正在测试以下方法:

    @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;
}

这是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);
}

这是错误:

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)
除了assertEquals之外,整个测试都有效。 一旦它尝试执行assertEquals,它就会抛出异常。

3 个答案:

答案 0 :(得分:1)

您是否覆盖了hashcode()的{​​{1}}和equals()以及toString()方法?

然而,projectile是因为Assertion errorexpResult不是result

答案 1 :(得分:1)

你应该纠正一些事情 -

  • 正如其他答案中所指出的,你应该在你的Projectile类中覆盖hashCode()和equals()方法。
  • 在单元测试中,您应该将对象传递给assertEquals(),而不是使用toString()进行比较。
  • 仅限覆盖toString()以使对象可读。

答案 2 :(得分:0)

覆盖类Projectile的toString方法。将你的等于改为

edittext.addTextChangedListener(new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        textView.setVisibility(View.VISIBLE);
    }

    public void afterTextChanged(Editable s) {
        if (s.length() == 0) {
          sendMenuItem.setVisible(true);
       } else {
          sendMenuItem.setVisible(false);
       }
    }
});

目前您正在测试实例是否相等