powermock-whennew不适用于简单的dto

g0czyy6m  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(226)

我知道这是个愚蠢的问题,但我不明白为什么会这样。我有测试代码,我试图理解powermockito.whennew方法是如何工作的。
简单dto user.class

public class User {

    private Long id;
    private String firstName;
    private String lastName;
    private int age;

    public User() {
    }

    public User(Long id, String firstName, String lastName, int age) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

简单试验

package ru.nikita.security.models;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNull;

@SpringBootTest
@RunWith(PowerMockRunner.class)
@PrepareForTest({User.class})
class UserTest {

    @Test
    void getId() throws Exception {
        PowerMockito.whenNew(User.class).withAnyArguments().thenReturn(null);
        User user = new User();

        assertNull(user);
    }
}

渐变相关性

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.3'
    testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.3'
    testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
}

我不明白为什么当我创建新用户时,它会创建简单的新对象。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题