Skip to content

Commit

Permalink
test: ensure value returned by constructor is recorded (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 authored May 2, 2023
1 parent 1b2bc12 commit 2f48d9a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
29 changes: 29 additions & 0 deletions trace-collector/src/test/java/NewCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,35 @@ void cloneArray_shouldOnlyCloneTheSpecifiedNumberOfElements() throws MavenInvoca
assertThat(arrayElementsAsValue.size(), equalTo(20));
}

@Test
void shouldRecordReturnValuesOfConstructor() throws MavenInvocationException, IOException {
// arrange
File pomFile = new File("src/test/resources/init/pom.xml");

// act
InvocationResult result = getInvocationResult(
pomFile,
List.of("methodsForExitEvent=src/test/resources/methods.json", "output=target/output.json"),
"-Dtest=MultipleConstructorTest#testMultipleConstructor");

// assert
assertThat(result.getExitCode(), equalTo(0));
File actualOutput = new File("src/test/resources/init/target/output.json");
assertThat(actualOutput.exists(), equalTo(true));

ObjectMapper mapper = new ObjectMapper();
SahabOutput output = mapper.readValue(actualOutput, new TypeReference<>() {});
assertThat(output.getReturns().size(), equalTo(2));

RuntimeReturnedValue returnedValue0 = output.getReturns().get(0);
assertThat(returnedValue0.getType(), equalTo("void"));
assertThat(returnedValue0.getValue(), is(nullValue()));

RuntimeReturnedValue returnedValue1 = output.getReturns().get(1);
assertThat(returnedValue1.getType(), equalTo("void"));
assertThat(returnedValue1.getValue(), is(nullValue()));
}

private InvocationResult getInvocationResult(File pomFile, List<String> agentOptions, String testArg)
throws MavenInvocationException, IOException {
// arrange
Expand Down
34 changes: 34 additions & 0 deletions trace-collector/src/test/resources/init/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>init</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bar;

public class MultipleConstructor {
public MultipleConstructor() {
}

public MultipleConstructor(String s) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import org.junit.jupiter.api.Test;

import bar.MultipleConstructor;


public class MultipleConstructorTest {

@Test
public void testMultipleConstructor() {
MultipleConstructor multipleConstructor = new MultipleConstructor();
MultipleConstructor multipleConstructor2 = new MultipleConstructor("foo");
System.out.println("Called both constructors");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"className": "bar/MultipleConstructor",
"name": "<init>"
}
]

0 comments on commit 2f48d9a

Please sign in to comment.