|
| 1 | +package com.codingame.codemachine.runner.junit; |
| 2 | + |
| 3 | +import com.codingame.codemachine.runner.junit.core.TestResultDto; |
| 4 | +import junit.framework.TestFailure; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.Description; |
| 7 | +import org.junit.runner.notification.Failure; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +public class TestResultProviderTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void should_create_TestResultDto_when_testFinished() throws Exception { |
| 20 | + String className = "com.codingame.test.MyTest"; |
| 21 | + String test = "theTest"; |
| 22 | + Description testDescription = Description.createTestDescription(className, test); |
| 23 | + |
| 24 | + TestResultDtoFactory testResultDtoFactory = mock(TestResultDtoFactory.class); |
| 25 | + List<TestResultDto> results = new ArrayList<>(); |
| 26 | + TestResultProvider testResultProvider = new TestResultProvider(results, testResultDtoFactory); |
| 27 | + TestResultDto testResultDto = new TestResultDto(); |
| 28 | + when(testResultDtoFactory.create(true, testDescription, null)).thenReturn(testResultDto); |
| 29 | + |
| 30 | + testResultProvider.testStarted(testDescription); |
| 31 | + String stdOutput = "This is a test output on stdout"; |
| 32 | + System.out.println(stdOutput); |
| 33 | + String stdError = "This is a test output on stderr"; |
| 34 | + System.err.println(stdError); |
| 35 | + testResultProvider.testFinished(testDescription); |
| 36 | + |
| 37 | + assertThat(results).hasSize(1); |
| 38 | + TestResultDto actualTestResultDto = results.get(0); |
| 39 | + assertThat(actualTestResultDto).isSameAs(testResultDto); |
| 40 | + assertThat(actualTestResultDto.getProgramStdout()).isEqualTo(stdOutput + "\n"); |
| 41 | + assertThat(actualTestResultDto.getProgramStderr()).isEqualTo(stdError + "\n"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void should_create_TestResultDto_when_testFailure() throws Exception { |
| 46 | + String className = "com.codingame.test.MyTest"; |
| 47 | + String test = "theTest"; |
| 48 | + Description testDescription = Description.createTestDescription(className, test); |
| 49 | + |
| 50 | + TestResultDtoFactory testResultDtoFactory = mock(TestResultDtoFactory.class); |
| 51 | + List<TestResultDto> results = new ArrayList<>(); |
| 52 | + TestResultProvider testResultProvider = new TestResultProvider(results, testResultDtoFactory); |
| 53 | + Throwable problem = new Throwable("Problem"); |
| 54 | + TestResultDto testResultDto = new TestResultDto(); |
| 55 | + when(testResultDtoFactory.create(false, testDescription, problem)).thenReturn(testResultDto); |
| 56 | + |
| 57 | + Failure failure = new Failure(testDescription, problem); |
| 58 | + testResultProvider.testStarted(testDescription); |
| 59 | + testResultProvider.testFailure(failure); |
| 60 | + testResultProvider.testFinished(testDescription); |
| 61 | + |
| 62 | + assertThat(results).hasSize(1); |
| 63 | + TestResultDto actualTestResultDto = results.get(0); |
| 64 | + assertThat(actualTestResultDto).isSameAs(testResultDto); |
| 65 | + } |
| 66 | +} |
0 commit comments