Как решить код ошибки в тестовом примере JUnit - PullRequest
0 голосов
/ 16 апреля 2020

У меня есть ошибка, связанная с кодом в этом классе. Я не знаю, достаточно ли это информации, поскольку это проект с несколькими интерфейсами и классами.

Это мой класс VideoHandler (подробности в комментариях):

 /**
 * (10pts) The VideoHandler is a Capability which provides the user 
 * interface with a way to display incoming video. We will not be doing 
 * real video, but instead simulating it with text-based terminal output, 
 * representing images as text pictures. The handler is capable of 
 * receiving notifications from a video source indicating that the video 
 * frame has been updated, and that it needs to retrieve the next frame 
 * and display it. 
 * @author jaybu
 *
 */
public class VideoHandler extends GeneralCapability implements NotificationListener {

    private VideoSource videoSource;

    /**
     * returns the VideoSource object associated with this capability, 
     * or null if there isn't an association yet.
     * @return
     */
    public VideoSource getVideoSource() {
        return (VideoSource) super.getSharable();
    }

    /**
     * returns true if the particular Capability understands how to 
     * process the given Sharable stream, and false otherwise
     * @param s
     * @return
     */
    @Override 
    public boolean recognizeSharable(Sharable s)  {
        return s instanceof VideoSource;
    }

    /**
     * in addition to linking the Sharable as in the GeneralCapability 
     * class, this method should set the VideoHandler as a 
     * NotificationListener for the VideoSource feed by using the 
     * appropriate method from the VideoSource class.
     */
    @Override 
    public void linkSharable(Sharable s) throws SharableException {
        super.linkSharable(s);
        ((VideoSource)getSharable()).setListener(this);

    }

    /**
     * in addition to unlinking the Sharable as in the GeneralCapability 
     * class, this method should deregister the VideoHandler as a 
     * NotificationListener from the VideoSource feed by using the 
     * appropriate method from VideoSource.
     * @param s
     */
    @Override 
    public void unlinkSharable(Sharable s) throws SharableException {
        super.unlinkSharable(s);
        if (getSharable() != null) {
            ((VideoSource)getSharable()).removeListener(this);   
        }
    }

    /**
     * if a VideoSource has been linked to the object, then this method 
     * will retrieve the new VideoSource contents, and print them out to 
     * the terminal (followed by a newline).
     */
    @Override
    public void sendNotification() {
        if (videoSource != null) {
            String contents = videoSource.getContents();
            System.out.println(contents);
        }
    }


}

Проблема связана с linkSharable , но я думаю, что это происходит из sendNotification , потому что я объявил объект private VideoSource videoSource;. В аналогичном классе ChatHandler у меня есть метод отправки, который выглядит следующим образом:

public class ChatHandler extends GeneralCapability implements MessageSink {
/**
 * (10pts) The ChatHandler class is a user interface Capability which 
 * specifically handles chat messages from a Chat object. We can use it to 
 * send out messages to others, or to print out messages which have been 
 * delivered.
 */


    /**
     * returns the Chat object associated with this capability, or 
     * null if there isn't an association yet.
     * @return
     */
    public Chat getChat() {
        return (Chat) super.getSharable();
    }

    /**
     * returns true only if the Sharable is a Chat.
     */
    @Override
    public boolean recognizeSharable(Sharable s) {
        return s instanceof Chat;
            // TODO Auto-generated method stub

    }

    /**
     *  in addition to linking the Sharable as in the GeneralCapability class, 
     *  this method should register the ChatHandler as a MessageSink for the 
     *  Chat feed by using its addSink method.
     * @throws SharableException 
     */
    @Override public void linkSharable(Sharable s) throws SharableException {
        super.linkSharable(s);
        ((Chat)getSharable()).addSink(this);//registers the ChatHandler

    }

    /**
     * in addition to unlinking the Sharable as in the GeneralCapability class, 
     * this method should deregister the ChatHandler as a MessageSink from the 
     * Chat feed by using its removeSink method.
     */
    @Override public void unlinkSharable(Sharable s) throws SharableException {
        super.unlinkSharable(s);
        if (getSharable()!= null){
            ((Chat)getSharable()).removeSink(this);//deregisters the ChatHandler    
        }
    }


    /**
     * if the Chat is associated, then this method will send the specified 
     * message to the Chat feed from the current user.
     * @param message
     */
    public void send(String message) {
        // TODO Auto-generated method stub
        if(getSharable() != null)
            ((Chat)getSharable()).send(getOwner(), message);
    }


    /**
     * will print out the incoming message in the format 
     * "USER: MESSAGE" (including a newline).
     */
    @Override
    public void deliver(Named sender, String message) {
        // TODO Auto-generated method stub
        System.out.println(sender.name() + ": " + message);
    }



}

Он очень похож на VideoHandler Я просто не знать, как go изменить VideoHandlers , sendNotification без использования объявленного объекта private VideoSource videoSource;

Это ошибка, которую я получаю для videohandler_link в моем тесте:

    org.junit.ComparisonFailure: VideoHandler should print contents of linked VideoSource when notified expected:<[testing123
]> but was:<[]>
    at org.junit.Assert.assertEquals(Assert.java:115)
    at P4Tester.videohandler_link(P4Tester.java:502)
    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:498)
    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.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
...