当前位置: 首页 > 知识库问答 >
问题:

MapStruct不能自动启动Spring-boot测试Gradle Junit5

农明辉
2023-03-14
@Mapper(unmappedTargetPolicy = ReportingPolicy.WARN,
        componentModel = "spring",
        injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface ItemMapper {
    ItemDto toDto(Item item);
    Item toItem(ItemDto itemDto);
}

public class ItemRepository {
    public ItemDto getItemDto() {
        return new ItemDto("item 1");
    }

    public Item getItem() {
        return new Item(1, "item 1", 20);
    }
}
@Service
@RequiredArgsConstructor
@Log4j2
public class ItemService {
    private final ItemRepository itemRepository;
    private final ItemMapper itemMapper;

    public ItemDto getItemDto() {
        Item item = itemRepository.getItem();
        ItemDto itemDto = itemMapper.toDto(item);
        log.info(itemDto);
        return itemDto;
    }

    public Item getItem() {
        ItemDto itemDto = itemRepository.getItemDto();
        Item item = itemMapper.toItem(itemDto);
        log.info(item);
        return item;
    }
}
@ExtendWith(MockitoExtension.class)
public class ItemServiceTest {

    @Mock
    private ItemRepository itemRepository;

    @InjectMocks
    private ItemService itemService;

    @Spy
    private ItemMapper itemMapper;

    @Test
    void shouldReturnItemDto() {
        Item mockItem = new Item(1, "mockItem", 10);
        given(itemRepository.getItem()).willReturn(mockItem);
        ItemDto itemDto = itemService.getItemDto();
        assertThat(mockItem.getName()).isEqualTo(itemDto.getName());
    }

}
plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.mapstruct.spring.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform {
    }
}

dependencies {
    // Mapstruct
    implementation 'org.mapstruct:mapstruct:1.3.0.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.0.Final'
    testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.3.0.Final'

    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation('org.springframework.boot:spring-boot-starter-test')

    // JUnit5
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.3.2'

    // Mockito
    testImplementation 'org.mockito:mockito-core:2.23.4'
    testImplementation 'org.mockito:mockito-junit-jupiter:2.23.4'
}

共有1个答案

刘阳舒
2023-03-14

@间谍注释

@Spy
private ItemMapper itemMapper;

不会执行生成的ItemMapperImpl类文件的依赖项注入。解决方案是设置

 @Spy
 private ItemMapper itemMapper = Mappers.getMapper(ItemMapper.class);

或者通过@springboottest(classes={itemmapperimpl.class})启用spring依赖项注入。

 类似资料:
  • 我有以下课程: 应用和配置类 MyRestController类 我的实用类 当我启动应用程序并将其作为独立jar运行时,或者从IDE(Eclipse)运行时,没有任何问题,一切都按预期进行。 然而,我想写一个单元测试来测试我的MyRestController类。。。我得到了一个NPE,因为自动连接字段util为null(在MyRestController类中)。 这是我的测试课: 我肯定错过了一

  • 我在spring TestContext配置的深潜过程中遇到了spring-boot-test问题。 示例项目:github示例项目 我有三个带有bean的Spring配置(-重量级配置,我需要缓存它) 两次然后 只有一次。在我看来,这是可能的,因为已经缓存为TestContext(发生这种情况是因为和包含在同一个包中) 这意味着spring启动我的spring上下文三次!为什么?在日志中查看三次

  • 通过Intellij执行的Spring Boot测试运行良好。但是当我用Maven运行测试时,它们失败了。 我使用这个Maven测试命令: 在surefire插件报告中找到的原因: ------------------------------------------------------------------------- 测试集:com.miro.project.controllers.Pr

  • 我有一个使用mvn构建的Spring Boot 2.25应用程序。根据本文件,我添加 从留档: 当DevTools监视类路径资源时,触发重启的唯一方法是更新类路径。导致类路径更新的方式取决于您正在使用的IDE。在Eclipse中,保存修改后的文件将导致类路径更新并触发重启。在IntelliJ IDEA中,构建项目(构建- 随着应用程序的运行,我尝试了一个简单的方法 希望应用程序重新启动,但什么也没

  • 我在网上看了很多关于< code > spring-boot-dev tools 的文章和问题,但是仍然不明白为什么它对我不起作用。每次运行我的应用程序,我都会得到以下信息: 每当我更改其中一个控制器文件时,什么也没发生。所以我遇到了一篇文章,提到我应该尝试将添加到我的应用程序属性文件中。使用 src 将不起作用,因为它会认为这是一个绝对路径,所以我将其更改为 。完成此操作后,将新endpoint

  • 我试图为一个Spring引导项目写一个集成测试。不幸的是,我对实现感到困惑。 下面是已尝试的示例代码段 问题 我是否需要一个单独的,带有注释以支持集成测试