https://github.com/benas/random-beans
):
<dependency>
<groupId>io.github.benas</groupId>
<artifactId>random-beans</artifactId>
<version>3.7.0</version>
<scope>test</scope>
</dependency>
豆子是:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String surname;
private Integer age;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Book> books;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String description;
private Integer pages;
这是我的测试代码(collectionsize限制为1):
https://github.com/cristianprofile/create-test-random-beans/blob/master/src/test/java/com/cromero/randombeaninstantationtesting/RandomBeanInstantationTestingApplicationTests.java#L41
EnhancedRandom random = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
.charset(forName("UTF-8"))
.stringLengthRange(5, 50)
.collectionSizeRange(1, 1)
.scanClasspathForConcreteTypes(true)
.overrideDefaultInitialization(false)
.build();
User user = random.random(User.class);
assertThat(user.getBooks()).hasSize(1);
我的测试在此断言中失败:
assertThat(user.getBooks()).hasSize(1);
为什么书的大小和我用随机对象配置的不一样。为什么?