001package edu.pdx.cs.joy.grader.poa; 002 003import org.junit.jupiter.api.BeforeEach; 004import org.junit.jupiter.api.Test; 005 006import java.io.IOException; 007import java.io.PrintWriter; 008import java.io.StringWriter; 009 010import static org.mockito.Mockito.mock; 011import static org.mockito.Mockito.verify; 012 013public class UnhandledExceptionPresenterTest extends EventBusTestCase { 014 015 private UnhandledExceptionView view; 016 017 @Override 018 @BeforeEach 019 public void setUp() { 020 super.setUp(); 021 this.unhandledExceptionHandler = this::doNotFailTestWhenUnhandledExceptionEncountered; 022 023 this.view = mock(UnhandledExceptionView.class); 024 new UnhandledExceptionPresenter(this.bus, this.view); 025 } 026 027 @Test 028 public void viewUpdatedOnUnhandledExceptionEvent() throws IOException { 029 String message = "This is an exception message"; 030 Throwable exception = new IllegalStateException(message).fillInStackTrace(); 031 032 this.bus.post(new UnhandledExceptionEvent(exception)); 033 034 verify(this.view).setExceptionMessage(message); 035 verify(this.view).setExceptionDetails(getStackTrace(exception)); 036 verify(this.view).displayView(); 037 } 038 039 private String getStackTrace(Throwable exception) throws IOException { 040 try (StringWriter sw = new StringWriter()) { 041 exception.printStackTrace(new PrintWriter(sw)); 042 sw.flush(); 043 return sw.toString(); 044 } 045 } 046}