Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring data mongodb - Optional cannot be returned by stream() in Mockito Test classes

I am developing Test cases for the Spring Data Mongo Services.

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Optional cannot be returned by stream()
stream() should return Stream
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

    at com.mastercard.customer.program.refdata.service.SubDivisionServiceTest.findAllSubDivisions_SuccessTest(SubDivisionServiceTest.java:168)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Here is the my sample code:

AggregationResults<EmployeeFacet> employeeFacets = null;
try {
    employeeFacets = mongoTemplate.aggregate(aggregation, Department.class, EmployeeFacet.class);
} catch (Exception ex) {
    log.error(AppConstants.ERROR, AppConstants.DB_ERROR, ex);
    throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, AppConstants.DB_ERROR, ex);
}

Another Code:

private SortOperation getSortOperation(Pageable pageable) {
        SortOperation sortOperation = null;
        Optional<Order> value = pageable.getSort().stream().findFirst();
        if(value.isPresent()) {
            Direction direction = value.get().getDirection();
            String property = value.get().getProperty();
            sortOperation = Aggregation.sort(direction, property); 
        }
        return sortOperation;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to fixed using below:

when(pageable.getSort()).thenReturn(Sort.by(order));

This works like charm !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...