Ich entwickle eine Spring Boot App. Die Service-Methode lädt ein PDF-Dokument mit Hilfe von GridFsTemplate, das im Service automatisch gesendet wird, in einen Mongoob Repo. Diese Datei-Upload-Service-Methode funktioniert wie erwartet über den Postman Rest Client. Aber, als ich versuchte, einen Komponententest zu laufen; Die SpringData GridFsTemplate wird nicht aufgerufen (in MongoDB können Sie GridFS zum Speichern von Binärdateien verwenden). Dies führt dazu, dass org.springframework.data.mongodb.gridfs.GridFsTemplate.store (...) eine NullPointerException auslöst. Bitte, kannst du mir helfen, ich bin seit ein paar Tagen darin stecken geblieben.GridFsTemplate NullPointerException in der Service Unit Test Klasse (Tech Stack: Spring Data/Spring Boot/Micro Service/Mongodb)
Unten ist mein Service-Implementierung:
@Service
public final class UploadServiceImpl implements UploadService {
@Autowired
private SequenceRepository sequenceDao;
@Autowired (required = true)
private GridFsTemplate gridFsTemplate;
@Override
public Long uploadFile(Invoice uploadedInvoice) {
ByteArrayInputStream byteArrayInputStream = null;
if (checkContentType(invoiceInfo.getContentType())) {
invoiceInfo.setPaymentID(sequenceDao.getNextSequenceId(INVOICE_UPLOAD_SEQ_KEY));
byteArrayInputStream = new ByteArrayInputStream(uploadedInvoice.getFileContent());
//Error thrown is java.lang.NullPointerException: null, where gridFsTemplate is null and basically autowire does not work when test is run.
GridFSFile gridFSUploadedFile= gridFsTemplate.store(byteArrayInputStream, invoiceInfo.getFileName(), invoiceInfo.getContentType(), invoiceInfo);
return 1l;
} else {
return 2l;
}
}
### Unten ist mein Unit-Test-Klasse für den Dienst
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration
public class UploadServiceTest {
@Mock
private SequenceRepository sequenceRepositoryMock;
@Autowired
private GridFsTemplate gridFsTemplateMock;
@Mock
private Invoice invoiceMock;
@InjectMocks
private static UploadService uploadService = new UploadServiceImpl();
DBObject fileMetaData = null;
DB db = null;
Jongo jongo = null;
@Before
public void setUp() throws Exception {
db = new Fongo("Test").getDB("Database");
jongo = new Jongo(db);
}
@Test
public void testUploadFile() {
//test 1
Long mockPaymentNo = new Long(1);
Mockito.when(sequenceRepositoryMock.getNextSequenceId(INVOICE_SEQUENCE)).thenReturn(mockPaymentNo);
assertEquals(mockPaymentNo, (Long) sequenceRepositoryMock.getNextSequenceId(INVOICE_SEQUENCE));
//test 2
Invoice dummyInvoice = getDummyInvoice();
InvoiceInfo dummyInvoiceInfo = dummyInvoice.getInvoiceInfo();
MongoCollection invoicesCollection = jongo.getCollection("invoices");
assertNotNull(invoicesCollection.save(dummyInvoiceInfo));
assertEquals(1, invoicesCollection.save(dummyInvoiceInfo).getN());
System.out.println("TEST 2 >>>>>>>>>>>>>>>>>> "+ uploadService);
//test 3 : The following line is the cause of the exception, the service method is called but the GridFsTemplate is not initialized when the test is run. But it works when the endpoint is invoked via postman
uploadService.uploadFile(dummyInvoice);
System.out.println("TEST 3 >>>>>>>>>>>>>>>>>> ");
}
}