存储库不是自动连线的,它是空的

Repository is not autowired, it is null

提问人:Tharindu Rangana 提问时间:11/1/2023 最后编辑:egleaseTharindu Rangana 更新时间:11/1/2023 访问量:29

问:

我已将spring boot版本升级到2.7.8。之后,当我尝试构建我的项目时,我在单元测试中遇到了错误。存储库未自动连接。它是 null。正因为如此,我遇到了以下问题。

public class CRUDAppServiceTest extends BaseTest {


    @Autowired
    private LocationRepository locationRepository;

    @Before
    public void setUp() {
        this.externalItemId = "abc";
        this.externalLocationId = "123";
        this.itemId = "xyz";
        final String locationId = "321";

        this.locationCreate(this.externalLocationId, locationId);
    }

    private void locationCreate(final String extPosLocationId, final String locationId) {
        final Map<String, String> attributes= new HashMap<>();
        attributes.put(ConnectorConstants.USE_86ING, "true");

        final MenuImportConfig menuImportConfig = new MenuImportConfig();
        menuImportConfig.setAttributes(attributes);

        final Location location = new Location();
        location.setExtPosLocationId(extPosLocationId);
        location.setLocationId(locationId);
        location.setMenuImportConfig(menuImportConfig);

        this.locationRepository.save(location);
    }

    @Test
    public void successTest() throws Exception {
        final PatchRequestDTO patchRequest = new PatchRequestDTO();
        patchRequest.setOp(Operation.TEST); // does not matter in this case
        patchRequest.setPath("stock");
        patchRequest.setValue("IN_STOCK");

        final List<PatchRequestDTO> patchRequests = Arrays.asList(patchRequest);

        this.mvc.perform(MockMvcRequestBuilders.patch("/item/" + this.itemId)
            .contentType(MediaType.APPLICATION_JSON)
            .content(GSON.toJson(patchRequests))).andExpect(MockMvcResultMatchers.status().isOk());
    }

}

获取successTestFAILED java.lang.NullPointerException

请解释一下我做错了什么以及如何纠正它? 另外,我在BaseTest类中使用了以下代码片段。

@AutoConfigureDataMongo
@AutoConfigureMockMvc
// empty in-memory MongoDB between each test
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {
    MyService.class
}, properties = {
    "order.notification.email.type=HTML",
    "aws.paramstore.enabled=false",
    "spring.main.banner-mode=off",
    "spring.mongodb.embedded.version=4.4.0"
})
@WebAppConfiguration
spring-boot unit-testing junit nullpointerexception 自动连线

评论


答: 暂无答案