Skip to content

Commit e5bf982

Browse files
author
Sylvain Fraïssé
committed
refact: extract a TestResultProvider
This class extends the `RunListener` and override the `testStarted`, `testFailure` and `testFinished` end points in order to create the `TestResultDto` according the test result.
1 parent 3f9661e commit e5bf982

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<version>3.6.1</version>
3333
<scope>test</scope>
3434
</dependency>
35+
<dependency>
36+
<groupId>org.mockito</groupId>
37+
<artifactId>mockito-core</artifactId>
38+
<version>2.6.8</version>
39+
</dependency>
3540
</dependencies>
3641

3742
<build>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.codingame.codemachine.runner.junit;
2+
3+
import com.codingame.codemachine.runner.junit.core.TestResultDto;
4+
import org.junit.runner.Description;
5+
import org.junit.runner.notification.Failure;
6+
import org.junit.runner.notification.RunListener;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.PrintStream;
10+
import java.util.List;
11+
12+
public class TestResultProvider extends RunListener {
13+
private final TestResultDtoFactory testResultDtoFactory;
14+
private final List<TestResultDto> results;
15+
16+
private ByteArrayOutputStream out;
17+
private ByteArrayOutputStream err;
18+
private TestResultDto currentResult;
19+
20+
TestResultProvider(List<TestResultDto> results) {
21+
this(results, null);
22+
}
23+
24+
TestResultProvider(List<TestResultDto> results, TestResultDtoFactory testResultDtoFactory) {
25+
this.results = results;
26+
this.testResultDtoFactory = testResultDtoFactory != null ? testResultDtoFactory : new TestResultDtoFactory();
27+
}
28+
29+
public void testStarted(Description description) {
30+
out = new ByteArrayOutputStream();
31+
err = new ByteArrayOutputStream();
32+
System.setOut(new PrintStream(out, true));
33+
System.setErr(new PrintStream(err, true));
34+
currentResult = null;
35+
}
36+
37+
public void testFailure(Failure failure) {
38+
currentResult = testResultDtoFactory.create(false, failure.getDescription(), failure.getException());
39+
}
40+
41+
public void testFinished(Description description) throws Exception {
42+
if (currentResult == null) {
43+
currentResult = testResultDtoFactory.create(true, description, null);
44+
}
45+
currentResult.setProgramStderr(new String(err.toByteArray()));
46+
currentResult.setProgramStdout(new String(out.toByteArray()));
47+
results.add(currentResult);
48+
err.close();
49+
out.close();
50+
}
51+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)