即使抛出预期的异常,junit测试也会失败

时间:2014-06-16 16:26:12

标签: java exception testing junit4

我有这个没有按预期工作的junit测试

@Test(expected = NoProviderAvailableException.class)
public void testNoProviderAvailableException() throws Exception {

    List<String> nodeSvc = new ArrayList<>();
    nodeSvc.add("addition");

    NodeManifest node = new NodeManifest("localhost", nodeSvc);

    ArithmeticServer arithmeticServer = new ArithmeticServer(managementClient.registerNode(node));
    arithmeticServer.start();       

    OperationDispatcher dispatcher = new OperationDispatcher(managementClient);
    //Thread.sleep(4);

    try {
        dispatcher.executeOperation("subtraction", 1, 2);
    } finally {
        arithmeticServer.stop();
    }
}

测试因java.lang.NullPointerException而失败,尽管它应该抛出预期的异常。如果我取消注释该行

//Thread.sleep(4);

测试抛出预期的异常。显然这不是问题的有力解决方案,所以我想知道什么是错的?

编辑:这里是OperationDispatcher的代码:

public class OperationDispatcher {

    private static Random random = new Random(10);

    public OperationDispatcher(ManagementService.Client managementClient) {
        this.managementClient = managementClient;
    }

    private ManagementService.Client managementClient;
    private ArithmeticService.Client arithmeticClient;  

    public long executeOperation(String opName, int num1, int num2) throws TException, NoProviderAvailableException {

        ServiceProvider provider = null;
        List<ServiceProvider> providers = managementClient.getProvidersForService(opName);

        long result = 0;

        if (providers.isEmpty())
            throw new NoProviderAvailableException();
        else {
            provider = providers.get(random.nextInt(providers.size()));

            TTransport arithmeticTransport = new TSocket(provider.getHostName(), provider.getPort());
            TProtocol arithmeticProtocol = new TBinaryProtocol(arithmeticTransport);
            arithmeticClient = new ArithmeticService.Client(arithmeticProtocol);
            arithmeticTransport.open();

            switch(opName) {
            case "addition":
                result = arithmeticClient.add(num1, num2);
                break;
            case "multiplication":
                result = arithmeticClient.multiply(num1, num2);
                break;
            case "subtraction":
                result = arithmeticClient.substract(num1, num2);
                break;
            case "division":
                result = arithmeticClient.divide(num1, num2);
                break;
            }

            arithmeticTransport.close();
        }

        return result;
    }

}

edit2:这里是堆栈跟踪:

  

[INFO]错误堆栈跟踪已打开。 [信息]正在扫描   projects ... [INFO] [INFO]使用构建器   org.apache.maven.lifecycle.internal.builder.singlethrea   ded.SingleThreadedBuilder,线程数为1 [INFO] [INFO]   -------------------------------------------------- ---------------------- [INFO]构建Apache Thrift示例1.0 [INFO]   -------------------------------------------------- ---------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:资源   (default-resources)@ thrift-exa mple --- [警告]使用平台   编码(实际上是Cp1252)来复制过滤后的资源,即构建是   平台依赖! [INFO]跳过不存在的resourceDirectory   c:\ Users \ Jacob \ workspace \ thrift-examp le \ src \ main \ resources [INFO]   [INFO] --- maven-compiler-plugin:2.5.1:compile(default-compile)@   thrift-exampl e --- [INFO]无需编译 - 所有类都可以   date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources   (default-testResources)@th rift-example --- [警告]使用平台   编码(实际上是Cp1252)来复制过滤后的资源,即构建是   平台依赖! [INFO]复制2个资源[INFO] [INFO] ---   maven-compiler-plugin:2.5.1:testCompile(default-testCompile)@ thrif   t-example --- [INFO]无需编译 - 所有类都是最新的   [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test(default-test)@   节俭 - 例子---

     

[INFO] Surefire报告目录:   c:\ Users \ Jacob \ workspace \ thrift-example \ target \ surefire-reports

     

----------------------------------------------- --------测试   -------------------------------------------------- -----运行it.uniud.atta.thrift.ManagementServiceTest启动管理服务器   在港口:7911 ......

     

听取请求......

     

在端口上关闭管理服务器:7911

     

测试运行:1,失败:0,错误:0,跳过:0,已过去时间:0.278   sec运行it.uniud.atta.thrift.OperationDispatcherTest测试运行:2,   失败:0,错误:1,跳过:0,经过的时间:0.921秒&lt;&lt;&lt; F A   ILURE!   testNoProviderAvailableException(it.uniud.atta.thrift.OperationDispatcherTest)   经过的时间:0.205秒&lt;&lt;&lt;错误! java.lang.Exception:意外   例外,预期但是           在org.junit.internal.runners.statements.ExpectException.evaluate(期待   Exception.java:31)           在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores。   Java的:28)           在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.ja)   VA:30)           在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)           在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRun)   ner.java:68)           在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRun)   ner.java:47)           在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)           在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)           在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)           在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)           在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)           在org.junit.runners.ParentRunner.run(ParentRunner.java:300)           在org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provide)   r.java:252)           at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4)   Provider.java:141)           在org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider)   的.java:112)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。   Java的:57)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces   sorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:606)           at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(   ReflectionUtils.java:189)           在org.apache.maven.surefire.booter.ProviderFactory $ ProviderProxy.invoke   (ProviderFactory.java:165)           在org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(Provi)   derFactory.java:85)           在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(Fork   edBooter.java:115)           在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:   75)引起:java.lang.NullPointerException           at it.uniud.atta.thrift.impl.ArithmeticServer.stop(ArithmeticServer.java)   :37)           at it.uniud.atta.thrift.OperationDispatcherTest.testNoProviderAvailableE   xception(OperationDispatcherTest.java:109)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。   Java的:57)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces   sorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:606)           at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(Framework   Method.java:45)           在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCal)   lable.java:15)           在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMe)   thod.java:42)           在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMet)   hod.java:20)           在org.junit.internal.runners.statements.ExpectException.evaluate(期待   Exception.java:22)           ......还有23个

     

结果:

     

错误测试:
  testNoProviderAvailableException(it.uniud.atta.thrift.OperationDispatcherTest)   :意外的异常,   预期但是   是

     

测试运行:3,失败:0,错误:1,跳过:0

     

[INFO]   -------------------------------------------------- ---------------------- [INFO] BUILD FAILURE [INFO]   -------------------------------------------------- ---------------------- [INFO]总时间:2.183 s [INFO]完成于:   2014-06-16T18:48:20 + 01:00 [信息]最终记忆:5M / 15M [INFO]   -------------------------------------------------- ---------------------- [错误]无法执行目标   org.apache.maven.plugins:Maven的万无一失-插件:2。   12.4:项目thrift上的测试(默认测试) - 示例:存在测试失败。 [错误] [错误]请参阅   c:\ Users \ Jacob \ workspace \ thrift-example \ target \ surefire-报告   个别测试结果。 [错误] - &gt; [帮助1]   org.apache.maven.lifecycle.LifecycleExecutionException:失败   执行目标o   rg.apache.maven.plugins:Maven的万无一失,插件:2.12.4:测试   项目thrift上的(默认测试)示例:测试失败。

     

请参阅   c:\ Users \ Jacob \ workspace \ thrift-example \ target \ surefire-reports for   个别测试结果。           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor)   的.java:212)           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor)   的.java:153)           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor)   的.java:145)           在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje   CT(LifecycleModuleBuilder.java:108)           在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje   CT(LifecycleModuleBuilder.java:76)           在org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThre   adedBuilder.build(SingleThreadedBuilder.java:51)           在org.apache.maven.lifecycle.internal.LifecycleStarter.execute(Lifecycl   eStarter.java:116)           在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)           在org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)           在org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)           在org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)           在org.apache.maven.cli.MavenCli.main(MavenCli.java:157)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。   Java的:57)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces   sorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:606)           在org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Laun   cher.java:289)           在org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.jav)   一:229)           在org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(La   uncher.java:415)           在org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:   356)引起:org.apache.maven.plugin.MojoFailureException:那里   是测试失败。

     

请参阅   c:\ Users \ Jacob \ workspace \ thrift-example \ target \ surefire-reports for   个别测试结果。           在org.apache.maven.plugin.surefire.SurefireHelper.reportExecution(Suref)   ireHelper.java:83)           在org.apache.maven.plugin.surefire.SurefirePlugin.writeSummary(Surefire)   Plugin.java:176)           在org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary(Surefir)   ePlugin.java:150)           在org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPre   conditionsChecked(AbstractSurefireMojo.java:650)           在org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(Abstrac)   tSurefireMojo.java:586)           在org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(默认   BuildPluginManager.java:133)           在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor)   的.java:208)           ......还有19个

0 个答案:

没有答案