我尝试在Sping Boot 中使用Point来实现junit测试。我完成了junit测试,但在AssignmentEntityBuilder的generateValidUserEntities方法中处理Point问题时遇到了问题。
我该如何解决?
下面是AssignmentEntity
...
public class AssignmentEntity extends BaseEntity {
...
public void setPoint(double latitude, double longitude) {
Coordinate coordinate = new Coordinate(latitude, longitude);
GeometryFactory geometryFactory = new GeometryFactory();
this.point = geometryFactory.createPoint(coordinate);
}
}
下面是AssignmentEntityBuilder
public class AssignmentEntityBuilder extends TestDataBuilder<AssignmentEntity> {
public AssignmentEntityBuilder() {
super(AssignmentEntity.class);
}
public static List<AssignmentEntity> generateValidUserEntities(int size) {
List<AssignmentEntity> userEntities = new ArrayList<>();
for (int i = 0; i < size; i++) {
AssignmentEntity assignmentEntity = new AssignmentEntityBuilder().withValidFields().build();
userEntities.add(assignmentEntity);
}
return userEntities;
}
public AssignmentEntityBuilder withValidFields() {
InstitutionEntity institutionEntity = new InstitutionEntityBuilder().withValidFields().build();
double x = Math.random();
double y = Math.random();
Point point = new GeometryFactory().createPoint(new Coordinate(x, y));
return this
.withId(AysRandomUtil.generateUUID())
.withInstitutionId(institutionEntity.getId())
.withInstitution(institutionEntity)
.withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build())
.withDescription("Description")
.withFirstName("First Name")
.withLastName("Last Name")
.withStatus(AssignmentStatus.AVAILABLE)
.withPoint(point);
}
public AssignmentEntityBuilder withId(String id) {
data.setId(id);
return this;
}
public AssignmentEntityBuilder withInstitutionId(String institutionId) {
data.setInstitutionId(institutionId);
return this;
}
public AssignmentEntityBuilder withPhoneNumber(AysPhoneNumber phoneNumber) {
data.setCountryCode(phoneNumber.getCountryCode());
data.setLineNumber(phoneNumber.getLineNumber());
return this;
}
public AssignmentEntityBuilder withInstitution(InstitutionEntity institution) {
data.setInstitution(institution);
return this;
}
public AssignmentEntityBuilder withDescription(String description) {
data.setDescription(description);
return this;
}
public AssignmentEntityBuilder withFirstName(String firstName) {
data.setFirstName(firstName);
return this;
}
public AssignmentEntityBuilder withLastName(String lastName) {
data.setLastName(lastName);
return this;
}
public AssignmentEntityBuilder withStatus(AssignmentStatus status) {
data.setStatus(status);
return this;
}
public AssignmentEntityBuilder withPoint(Point point) {
data.setPoint(point);
return this;
}
}
下面是服务类
class AssignmentSaveServiceImplTest extends AbstractUnitTest {
@InjectMocks
private AssignmentSaveServiceImpl assignmentSaveService;
@Mock
private AssignmentRepository assignmentRepository;
@Mock
private AysIdentity identity;
@Test
void givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment() {
// Given
AssignmentSaveRequest mockAssignmentSaveRequest = new AssignmentSaveRequestBuilder()
.withValidFields()
.build();
List<AssignmentEntity> assignmentsFromDatabase = AssignmentEntityBuilder.generateValidUserEntities(10); --- ERROR LINE
AssignmentEntity mockAssignmentEntity = new AssignmentEntityBuilder()
.withValidFields().build();
// When
Mockito.when(assignmentRepository.findAll())
.thenReturn(assignmentsFromDatabase);
Mockito.when(assignmentRepository.save(Mockito.any(AssignmentEntity.class)))
.thenReturn(mockAssignmentEntity);
Mockito.when(identity.getInstitutionId())
.thenReturn(AysRandomUtil.generateUUID());
// Then
Assignment assignment = assignmentSaveService.saveAssignment(mockAssignmentSaveRequest);
Assertions.assertEquals(mockAssignmentSaveRequest.getFirstName(), assignment.getFirstName());
Assertions.assertEquals(mockAssignmentSaveRequest.getLastName(), assignment.getLastName());
Assertions.assertEquals(mockAssignmentSaveRequest.getPhoneNumber().getLineNumber(), assignment.getPhoneNumber().getLineNumber());
Assertions.assertEquals(mockAssignmentSaveRequest.getPhoneNumber().getCountryCode(), assignment.getPhoneNumber().getCountryCode());
Assertions.assertEquals(mockAssignmentSaveRequest.getDescription(), assignment.getDescription());
Assertions.assertEquals(mockAssignmentSaveRequest.getDescription(), assignment.getDescription());
Assertions.assertEquals(mockAssignmentSaveRequest.getLongitude(), assignment.getLongitude());
Assertions.assertEquals(mockAssignmentSaveRequest.getLatitude(), assignment.getLatitude());
Mockito.verify(assignmentRepository, Mockito.times(1)).findAll();
Mockito.verify(identity, Mockito.times(1)).getInstitutionId();
}
}
下面是显示的错误
org.jeasy.random.ObjectCreationException: Unable to create a random instance of type class com.ays.assignment.model.entity.AssignmentEntity
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:172)
at org.jeasy.random.EasyRandom.nextObject(EasyRandom.java:100)
at com.ays.common.model.TestDataBuilder.<init>(TestDataBuilder.java:39)
at com.ays.common.model.TestDataBuilder.<init>(TestDataBuilder.java:33)
at com.ays.assignment.model.entity.AssignmentEntityBuilder.<init>(AssignmentEntityBuilder.java:20)
at com.ays.assignment.model.entity.AssignmentEntityBuilder.generateValidUserEntities(AssignmentEntityBuilder.java:31)
at com.ays.assignment.service.impl.AssignmentSaveServiceImplTest.givenValidAssignmentSaveRequest_whenAssignmentSaved_thenReturnAssignment(AssignmentSaveServiceImplTest.java:41)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.jeasy.random.ObjectCreationException: Unable to create type: org.locationtech.jts.geom.Point for field: point of class: com.ays.assignment.model.entity.AssignmentEntity
at org.jeasy.random.FieldPopulator.populateField(FieldPopulator.java:98)
at org.jeasy.random.EasyRandom.populateField(EasyRandom.java:209)
at org.jeasy.random.EasyRandom.populateFields(EasyRandom.java:198)
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:165)
... 74 more
Caused by: org.jeasy.random.ObjectCreationException: Unable to create a random instance of type class org.locationtech.jts.geom.Point
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:172)
at org.jeasy.random.FieldPopulator.generateRandomValue(FieldPopulator.java:160)
at org.jeasy.random.FieldPopulator.populateField(FieldPopulator.java:93)
... 77 more
Caused by: org.jeasy.random.ObjectCreationException: Unable to create type: org.locationtech.jts.geom.CoordinateSequence for field: coordinates of class: org.locationtech.jts.geom.Point
at org.jeasy.random.FieldPopulator.populateField(FieldPopulator.java:98)
at org.jeasy.random.EasyRandom.populateField(EasyRandom.java:209)
at org.jeasy.random.EasyRandom.populateFields(EasyRandom.java:198)
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:165)
... 79 more
Caused by: org.jeasy.random.ObjectCreationException: Unable to create a random instance of type interface org.locationtech.jts.geom.CoordinateSequence
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:172)
at org.jeasy.random.FieldPopulator.generateRandomValue(FieldPopulator.java:160)
at org.jeasy.random.FieldPopulator.populateField(FieldPopulator.java:93)
... 82 more
Caused by: org.jeasy.random.ObjectCreationException: Unable to create an instance of type: interface org.locationtech.jts.geom.CoordinateSequence
at org.jeasy.random.ObjenesisObjectFactory.createInstance(ObjenesisObjectFactory.java:67)
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:147)
... 84 more
Caused by: java.lang.InstantiationError: org.locationtech.jts.geom.CoordinateSequence
at jdk.internal.reflect.GeneratedSerializationConstructorAccessor7.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:484)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
at org.jeasy.random.ObjenesisObjectFactory.createNewInstance(ObjenesisObjectFactory.java:78)
at org.jeasy.random.ObjenesisObjectFactory.createInstance(ObjenesisObjectFactory.java:65)
... 85 more
1条答案
按热度按时间wgxvkvu91#
下面是解决方案展示
**1 -**从
AbstractRandomizer
扩展定义一个名为PointRandomizer
的类,随机生成Point。**2 -**定义一个名为
PointRandomizer
的静态final变量**3 -**将
POINT_RANDOMIZER
与EasyRandomParameters
相加,随机产生点值下面是最终的
TestBuilder
类