Skip to content

Commit b9de2d7

Browse files
committed
1 parent a1b9db7 commit b9de2d7

File tree

6 files changed

+232
-1
lines changed

6 files changed

+232
-1
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# instancio-github-issues
2-
Reproducible projects for Github Issues
2+
3+
Reproducible projects for GitHub issues:
4+
5+
https://github.com/instancio/instancio/issues

issue-213/pom.xml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.instancio.github</groupId>
5+
<artifactId>issue-213</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<java.version>8</java.version>
11+
<lombok.version>1.18.22</lombok.version>
12+
<commons.lang3.version>3.12.0</commons.lang3.version>
13+
<instancio.version>1.5.4</instancio.version>
14+
<junit.version>5.9.0</junit.version>
15+
<assertj.version>3.23.1</assertj.version>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-lang3</artifactId>
23+
<version>${commons.lang3.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.projectlombok</groupId>
27+
<artifactId>lombok</artifactId>
28+
<version>${lombok.version}</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.instancio</groupId>
33+
<artifactId>instancio-junit</artifactId>
34+
<version>${instancio.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.assertj</groupId>
39+
<artifactId>assertj-core</artifactId>
40+
<version>${assertj.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter</artifactId>
45+
<version>${junit.version}</version>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.10.1</version>
56+
<configuration>
57+
<source>${java.version}</source>
58+
<target>${java.version}</target>
59+
<annotationProcessorPaths>
60+
<path>
61+
<groupId>org.projectlombok</groupId>
62+
<artifactId>lombok</artifactId>
63+
<version>${lombok.version}</version>
64+
</path>
65+
</annotationProcessorPaths>
66+
</configuration>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-surefire-plugin</artifactId>
71+
<version>3.0.0-M7</version>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example;
2+
3+
import lombok.Value;
4+
import org.apache.commons.lang3.builder.ToStringBuilder;
5+
import org.apache.commons.lang3.builder.ToStringStyle;
6+
7+
import java.time.LocalDateTime;
8+
9+
@Value
10+
public class Foo {
11+
String fooValue;
12+
LocalDateTime lastModified;
13+
LocalDateTime createdOn;
14+
15+
@Override
16+
public String toString() {
17+
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.example;
2+
3+
import lombok.Value;
4+
import org.apache.commons.lang3.builder.ToStringBuilder;
5+
import org.apache.commons.lang3.builder.ToStringStyle;
6+
7+
import java.time.LocalDateTime;
8+
import java.util.List;
9+
10+
@Value
11+
public class Person {
12+
String firstName;
13+
String fullName;
14+
List<String> hobbies;
15+
LocalDateTime lastModified;
16+
LocalDateTime createdOn;
17+
18+
@Override
19+
public String toString() {
20+
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.example;
2+
3+
import org.instancio.Instancio;
4+
import org.instancio.Model;
5+
6+
import java.time.LocalDateTime;
7+
8+
import static org.instancio.Select.all;
9+
import static org.instancio.Select.field;
10+
11+
/**
12+
* Constructs test object Models in a meaningful state.
13+
* The returned models can be customised further, if required.
14+
*/
15+
class Factory {
16+
17+
/**
18+
* A model for classes that have 'lastModified' and 'createdOn' fields.
19+
* Note: passed in class must have these fields or an error will be thrown.
20+
*/
21+
static <T> Model<T> commonModel(Class<T> klass) {
22+
LocalDateTime lastModified = Instancio.of(LocalDateTime.class)
23+
.generate(all(LocalDateTime.class), gen -> gen.temporal().localDateTime().past())
24+
.create();
25+
26+
return Instancio.of(klass)
27+
.set(field("lastModified"), lastModified)
28+
.generate(field("createdOn"), gen -> gen.temporal().localDateTime().range(LocalDateTime.MIN, lastModified))
29+
.toModel();
30+
}
31+
32+
static Model<Person> personModel() {
33+
String firstName = Instancio.create(String.class);
34+
35+
// Use the common model as a template for the Person class
36+
Model<Person> personModel = commonModel(Person.class);
37+
38+
// Create a Person model and customise it further.
39+
return Instancio.of(personModel)
40+
.set(field("firstName"), firstName)
41+
.generate(field("fullName"), gen -> gen.string().prefix(firstName + " "))
42+
.toModel();
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.example;
2+
3+
import org.instancio.Instancio;
4+
import org.instancio.Model;
5+
import org.instancio.junit.InstancioExtension;
6+
import org.instancio.junit.Seed;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.RepeatedTest;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.instancio.Select.field;
14+
15+
@ExtendWith(InstancioExtension.class)
16+
class GithubIssueSampleTest {
17+
18+
@Test
19+
@DisplayName("Create Foo from model")
20+
void createFooFromModel() {
21+
Model<Foo> fooModel = Factory.commonModel(Foo.class);
22+
Foo foo = Instancio.create(fooModel);
23+
24+
assertThat(foo.getLastModified()).isAfterOrEqualTo(foo.getCreatedOn());
25+
}
26+
27+
@Test
28+
@DisplayName("Create Person from model")
29+
void createPersonFromModel() {
30+
Model<Person> personModel = Factory.personModel();
31+
Person person = Instancio.create(personModel);
32+
33+
assertThat(person.getLastModified()).isAfterOrEqualTo(person.getCreatedOn());
34+
assertThat(person.getFullName()).startsWith(person.getFirstName() + " ");
35+
assertThat(person.getHobbies()).isNotEmpty();
36+
}
37+
38+
@Test
39+
@DisplayName("Create a customised Person from model")
40+
void createCustomPersonFromModel() {
41+
Model<Person> personModel = Factory.personModel();
42+
43+
// Customise the person before creating an instance from the model
44+
Person person = Instancio.of(personModel)
45+
.generate(field("hobbies"), gen -> gen.collection().with("music", "movies"))
46+
.create();
47+
48+
assertThat(person.getLastModified()).isAfterOrEqualTo(person.getCreatedOn());
49+
assertThat(person.getFullName()).startsWith(person.getFirstName() + " ");
50+
assertThat(person.getHobbies()).contains("music", "movies");
51+
}
52+
53+
/**
54+
* Since we have multiple 'Instancio.create()' calls in the Factory class,
55+
* to ensure they all use same the seed, we can set it using the annotation.
56+
*/
57+
@Seed(1)
58+
@RepeatedTest(3)
59+
@DisplayName("Should generate the same person data on each test run")
60+
void createCustomPersonFromModelWithStaticData_usingAnnotation() {
61+
Person person = Instancio.of(Factory.personModel())
62+
.generate(field("hobbies"), gen -> gen.collection().with("travel"))
63+
.create();
64+
65+
System.out.println(person);
66+
}
67+
}

0 commit comments

Comments
 (0)