Skip to content

Commit

Permalink
+Test first: New Testcase for Issue #70
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSchankin committed Jun 30, 2021
1 parent 5ff22ec commit 34c07ff
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id" : "6ff9ee24-f86a-4b8e-becc-7e3c46dd6e45",
"sampleMethodToSampleMap" : {
"public de.ppi.deepsampler.provider.common.Animal de.ppi.deepsampler.provider.common.TestService.getAnimal()" : {
"callMap" : [ {
"parameter" : {
"args" : [ ]
},
"returnValue" : {
"@type" : "de.ppi.deepsampler.persistence.bean.DefaultPersistentBean",
"values" : {
"0$name" : "Porthos"
}
}
} ]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.ppi.deepsampler.provider.common;

public interface Animal {

String getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.ppi.deepsampler.provider.common;

public class Dog implements Animal {

private String name;

public Dog(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1196,4 +1196,41 @@ public void mixPureJavaApiAndPersistenceApi() throws IOException {
Files.delete(Paths.get(pathToFile));
}


@Test
public void polymorphicSamplesCanBeRecordedAndLoaded() throws IOException {
// Ensure, that no Samples from previous tests exists...
Sampler.clear();

// GIVEN
final TestService testServiceSampler = Sampler.prepare(TestService.class);
Sample.of(testServiceSampler.getAnimal());

// make the method call that is recorded
getTestService().getAnimal();

// save the recorded sample to file...
final String pathToFile = "./record/polymorphicSamplesCanBeRecordedAndLoaded.json";
final PersistentSampleManager source = PersistentSampler.source(JsonSourceManager.builder().buildWithFile(pathToFile));
source.record();

// Reset SampleRepository to ensure that only deserialized samples will be used in the next steps...
assertFalse(SampleRepository.getInstance().isEmpty());
Sampler.clear();
assertTrue(SampleRepository.getInstance().isEmpty());

// deserialize the sample...
Sample.of(testServiceSampler.getAnimal());
source.load();

// THEN
assertFalse(SampleRepository.getInstance().isEmpty());
assertNotNull(getTestService().getAnimal());
// Although only the interface was declared in the sampled method, we expect to see the concrete return type Dog:
assertTrue(getTestService().getAnimal() instanceof Dog);
assertEquals("Porthos", getTestService().getAnimal().getName());

Files.delete(Paths.get(pathToFile));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,8 @@ public Map<Integer, Integer> getMapOfIntegers() {

return map;
}

public Animal getAnimal() {
return new Dog("Porthos");
}
}

0 comments on commit 34c07ff

Please sign in to comment.