powermock-whennew不适用于简单的dto

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

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

  1. public class User {
  2. private Long id;
  3. private String firstName;
  4. private String lastName;
  5. private int age;
  6. public User() {
  7. }
  8. public User(Long id, String firstName, String lastName, int age) {
  9. this.id = id;
  10. this.firstName = firstName;
  11. this.lastName = lastName;
  12. this.age = age;
  13. }
  14. }

简单试验

  1. package ru.nikita.security.models;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.runner.RunWith;
  4. import org.powermock.api.mockito.PowerMockito;
  5. import org.powermock.core.classloader.annotations.PrepareForTest;
  6. import org.powermock.modules.junit4.PowerMockRunner;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import static org.junit.jupiter.api.Assertions.assertNull;
  9. @SpringBootTest
  10. @RunWith(PowerMockRunner.class)
  11. @PrepareForTest({User.class})
  12. class UserTest {
  13. @Test
  14. void getId() throws Exception {
  15. PowerMockito.whenNew(User.class).withAnyArguments().thenReturn(null);
  16. User user = new User();
  17. assertNull(user);
  18. }
  19. }

渐变相关性

  1. dependencies {
  2. implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
  3. implementation 'org.springframework.boot:spring-boot-starter-security'
  4. implementation 'org.springframework.boot:spring-boot-starter-web'
  5. compileOnly 'org.projectlombok:lombok'
  6. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  7. annotationProcessor 'org.projectlombok:lombok'
  8. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  9. testImplementation 'org.springframework.security:spring-security-test'
  10. testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.3'
  11. testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.3'
  12. testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
  13. }

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

暂无答案!

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

相关问题