首页 > 解决方案 > 无法调用“org.apache.commons.logging.Log.isDebugEnabled()”,因为“this.logger”为空

问题描述

这里是我用来保存数据的代码,我使用@Transactional. 我想为此方法编写一个测试,但它失败了。

  @Transactional(rollbackFor = BadRequestException.class, propagation = Propagation.REQUIRES_NEW)
  public void saveContent(ContentAbstractEntity content) {
      
    mongoTemplate.setSessionSynchronization(SessionSynchronization.ALWAYS);

    TransactionBody txnBody =
        (() -> {
          ContentAbstractEntity contentAbstractEntity = contentRepository.save(content);

          if (content instanceof LearningContent) {

            LearningContent learningContent = (LearningContent) content;

            Long tutorCountInDB = tutorRepository.countBy_idIn(learningContent.getTutorIds());

            if (!countryRepository.existsBySyllabuses_grades_subjects__id(
                learningContent.getSubjectId()))
              throw new BadRequestException("Subject is not found");

            Topic topic = topicRepository.findBy_id(learningContent.getTopicId());

            if (topic == null) throw new BadRequestException("Topic is not found");

            List<ObjectId> lessonIds =
                topic.getLessons().stream().map(m -> m.get_id()).collect(Collectors.toList());

            boolean allLessonsExists =
                learningContent.getLessonIds().stream().allMatch(a -> lessonIds.contains(a));

            if (!allLessonsExists) throw new BadRequestException("Lessons are not mismatched");

            if (tutorCountInDB != learningContent.getTutorIds().size()) {
              throw new BadRequestException("Tutors are not matched");
            }

       
          }

          return "Successfully inserted";
        });

    MongoClient client = MongoClients.create(uri);
    ClientSession clientSession = client.startSession();

    try {

      clientSession.withTransaction(txnBody);

    } catch (BadRequestException e) {

      throw new BadRequestException(e.getMessage());

    } finally {

      clientSession.close();
    }
  }

我试图为上面的代码写一个测试,但它返回一个错误

  @Test
  void whenSaveContent() throws Exception {

    ContentAbstractDto contentAbstractDto = new ContentAbstractDto();

    ContentAbstractEntity content = learningVideo();

    if (contentAbstractDto instanceof LearningDocsDto) {

      LearningDocsDto learninfDocsDto = (LearningDocsDto) contentAbstractDto;

      // ToDo when a Doc Saves and Assessment saves
    }

    when(contentRepository.save(content)).thenReturn(content);

    if (content instanceof LearningContent) {

      LearningContent learningContent = (LearningContent) content;

      Long tutorCountInDB = (long) learningContent.getTutorIds().size();
      when(tutorRepository.countBy_idIn(learningContent.getTutorIds())).thenReturn(tutorCountInDB);

      when(subjectRepository.existsBy_id(learningContent.getSubjectId())).thenReturn(true);

      Topic topic = getTopic();
      when(topicRepository.findBy_id(learningContent.getTopicId())).thenReturn(topic);

      if (topic == null) throw new BadRequestException("Topic is not found");

      List<ObjectId> lessonIds =
          topic.getLessons().stream().map(m -> m.get_id()).collect(Collectors.toList());

      boolean allLessonsExists =
          learningContent.getLessonIds().stream().allMatch(a -> lessonIds.contains(a));

      if (!allLessonsExists) throw new BadRequestException("Lessons are not mismatched");

      if (tutorCountInDB != learningContent.getTutorIds().size()) {
        throw new BadRequestException("Tutors are not matched");
      }
  
    }

标签: spring-boottestcase

解决方案


推荐阅读