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

Mockito在Spring测试类@BeforeAll方法中抛出NPE

常枫涟
2023-03-14
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
public class VirtualMachineServiceIntegrationTests {

    @MockBean
    static VirtualMachineService virtualMachineService;

    @Autowired
    private MockMvc mockMvc;
    @Autowired
    private ObjectMapper objectMapper;

    static String courseId;
    static String studentId;
    static Long teamId;
    static Long vmId;
    static SystemImage os;
    static VirtualMachineDTO virtualMachineDTO;
    static ConfigurationDTO configurationDTO;


    @BeforeAll
    static void beforeAll() {
        studentId = "student";
        courseId = "course";
        teamId = 1L;
        vmId = 1L;
        os = SystemImage.WINDOWS_10;
        virtualMachineDTO = VirtualMachineDTO.builder()
                .id(1L)
                .num_vcpu(2)
                .disk_space(1000)
                .ram(4)
                .status(VirtualMachineStatus.OFF)
                .build();
        configurationDTO = ConfigurationDTO.builder()
                .id(1L)
                .min_vcpu(2)
                .min_disk(1000)
                .min_ram(4)
                .max_vcpu(12)
                .max_disk(2000)
                .max_ram(16)
                .max_on(4)
                .tot(20)
                .build();

        Mockito.when(Mockito.any(VirtualMachineService.class).createVirtualMachine(Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(virtualMachineDTO);
    }
}
 Mockito.when(Mockito.any(VirtualMachineService.class).createVirtualMachine(Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(virtualMachineDTO);
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)
-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)
-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)
-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)
-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)
-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)
-> at it.polito.ai.backend.VirtualMachineServiceIntegrationTests.createVirtualMachine(VirtualMachineServiceIntegrationTests.java:87)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

共有1个答案

通安宁
2023-03-14

这里有很多事情有点奇怪。主要/误解问题是:

mockito#when用于告诉具体的mock在特定的方法调用上做什么。所以通常情况下是这样的:

Mockito.when(mock.doSomeThings(anyString()).thenReturn("whatever");

将此模式应用到代码中,应该可以实现以下目的:

Mockito.when(virtualMachineService.createVirtualMachine(Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(), Mockito.any Int(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(virtualMachineDTO);
 类似资料:
  • 我正在用Mockito为一个项目设置jUnit测试。在被测系统(DrawingService)中调用方法时遇到问题。模拟包括图形和IDrawingRepository。 我不熟悉TDD、单元测试和一般的模拟,所以我可能犯了一些noob错误?任何帮助都将不胜感激。 以下是测试课程: 以下是正在测试的系统:

  • 因为这个方法是在运行时执行的,所以需要对它进行测试吗

  • 您好,我有一个将人员添加到团队的方法。我想为此方法编写一个测试,但我是junit/mockito测试的新手,所以我有很多问题:这是我的添加方法: 这两个实体(人员/团队)之间存在关系,这是我的测试代码,但它不起作用: 模拟组合:

  • 如何使用JUnit5测试抛出NullPointerException的方法。但是我在方法中捕获到了这个异常,因此test以错误结尾:“Expected java.lang.NullPointerException to be thrown,But nothing was thrown”。 谢谢

  • 我正试图为一个方法编写一个测试用例,该方法基于特定的逻辑抛出异常。然而,测试用例失败了,因为预期的异常和获得的异常是不同的。 我如何解决这个问题?

  • 我有一个类,它有一个方法。我正在做相应的测试,但是我还不能验证是否抛出了定制的异常,我使用的是JUnit5。 我已经复习了这里,但答案并没有帮助我,这是我根据一个示例编写的代码: