预期会抛出 java.lang.IllegalArgumentException,但实际上没有抛出任何异常。

huangapple go评论72阅读模式
英文:

expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown

问题

我写了一个解决两条DNA链之间汉明距离的代码,并且使用了JUnit的assertThrows方法。当两条链的长度不同时,预期会抛出IllegalArgumentException,但我得到了以下错误:

预期会抛出java.lang.IllegalArgumentException,但实际上没有抛出任何异常

我漏掉了什么?你能帮我看看吗?提前感谢你的回答。

测试方法:

@Test
public void testValidatesFirstStrandNotLonger() {
    IllegalArgumentException expected =
        assertThrows(
            IllegalArgumentException.class,
            () -> new Hamming("AATG", "AAA"));

    assertThat(expected)
        .hasMessage("leftStrand and rightStrand must be of equal length.");
}

代码:

public class Hamming {
    private String leftStrand;
    private String rightStrand;

    public Hamming(String leftStrand, String rightStrand) {
        this.leftStrand = leftStrand;
        this.rightStrand = rightStrand;
    }

    public int getHammingDistance() {
        if (leftStrand.isEmpty() || rightStrand.isEmpty() || leftStrand.equals(rightStrand)) {
            return 0;
        }

        if (leftStrand.length() == rightStrand.length()) {
            int counter = 0;
            for (int i = 0; i < leftStrand.length(); i++)
                if (leftStrand.charAt(i) != rightStrand.charAt(i))
                    counter++;

                return counter;
        } else {
            throw new IllegalArgumentException();
        }
    }
}

错误信息:

预期会抛出java.lang.IllegalArgumentException,但实际上没有抛出任何异常
java.lang.AssertionError: 预期会抛出java.lang.IllegalArgumentException,但实际上没有抛出任何异常
    at org.junit.Assert.assertThrows(Assert.java:1028)
    at org.junit.Assert.assertThrows(Assert.java:981)
    at HammingTest.testValidatesFirstStrandNotLonger(HammingTest.java:42)
    ...
英文:

I wrote a code that solves the hamming distance between two DNA strands and I used JUnit assertThrows method. it should have been thrown IllegalArgumentException when two strands have different length but I'm getting

expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown

what am I missing can you look at this, thanks for your answers in advance

The test method:

    @Test
    public void testValidatesFirstStrandNotLonger() {
        IllegalArgumentException expected =
            assertThrows(
                IllegalArgumentException.class,
                () -&gt; new Hamming(&quot;AATG&quot;, &quot;AAA&quot;));

        assertThat(expected)
            .hasMessage(&quot;leftStrand and rightStrand must be of equal length.&quot;);
    }

The code:


public class Hamming {
    private String leftStrand;
    private String rightStrand;

    public Hamming(String leftStrand, String rightStrand) {
        this.leftStrand = leftStrand;
        this.rightStrand = rightStrand;
    }

    public int getHammingDistance() {
        if (leftStrand.isEmpty() || rightStrand.isEmpty() || leftStrand.equals(rightStrand)) {
            return 0;
        }

        if (leftStrand.length() == rightStrand.length()) {
            int counter = 0;
            for (int i = 0; i &lt; leftStrand.length(); i++)
                if (leftStrand.charAt(i) != rightStrand.charAt(i))
                    counter++;

                return counter;
        } else {
            throw new IllegalArgumentException();
        }


    }

}

The error message:

expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown
java.lang.AssertionError: expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown
	at org.junit.Assert.assertThrows(Assert.java:1028)
	at org.junit.Assert.assertThrows(Assert.java:981)
	at HammingTest.testValidatesFirstStrandNotLonger(HammingTest.java:42)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:119)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
	at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
	at java.base/java.lang.Thread.run(Thread.java:834)

答案1

得分: 4

你预期的异常由getHammingDistance()方法抛出。但在你的测试中,你只调用了构造函数,它不会抛出任何异常。另外,请注意hasMessage("leftStrand和rightStrand的长度必须相等。")仍然会失败,因为IllegalArgumentException()在没有消息的情况下被抛出。

英文:

The exception you expect is thrown by the getHammingDistance() method. But in your test you only call the constructor, which throws nothing. Also, note that hasMessage(&quot;leftStrand and rightStrand must be of equal length.&quot;) will still fail, since IllegalArgumentException() is thrown without a message.

huangapple
  • 本文由 发表于 2020年8月24日 20:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63561526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定