【问题标题】:Spring Framework - Mock is not mockingSpring Framework - 模拟不是模拟
【发布时间】:2020-10-14 15:20:04
【问题描述】:

我有一个 Spring MvC 应用程序。 (Spring Framework是Java平台的应用框架和反转控制容器)用这个测试:

    @RunWith(SpringJUnit4ClassRunner.class)
    public class AutorisationServiceTest {
    
        @Autowired
        @InjectMocks
        private IAutorisationService service;
    
        @Mock
        PersonneRepository personneRepository = Mockito.mock(PersonneRepository.class);
    
    
        @Before
        public void setup() {
        }
    
    
        @Test
        public void should_Find_GardeWith2Affectations() throws IOException {
    
            when(personneRepository.getAll(anyString())).thenReturn(DataLoader.mockData());
            service.getAll("rules");
        }
    
    }

    @Service
    public class AutorisationService implements IAutorisationService {
    
        private final PersonneRepository personneRepository;
    
        public AutorisationService(PersonneRepository personneRepository) {
            this.personneRepository = personneRepository;
        }

@Override
    public List<User> getAll(String where) {
        return personneRepository.getAll(where));
    }
    ...
    }

但是当我运行测试时,似乎没有模拟 repo

【问题讨论】:

  • 您使用的是哪个junit。 Junit4 还是 Junit5?你能用导入显示代码吗?
  • 我使用的是 4.13
  • 您正在模拟最终对象。模拟最终对象通常会返回 null。您可以尝试将其从 final 更改。

标签: java spring spring-mvc junit mockito


【解决方案1】:

您应该决定是要编写 Spring 测试还是 Mockito 测试。

弹簧测试将加载完整的上下文,然后您可以使用 @Autowired@MockBean

@RunWith(SpringJUnit4ClassRunner.class)
public class AutorisationServiceTest {
    
  @Autowired
  private IAutorisationService service;
    
  @MockBean
  PersonneRepository personneRepository;

Mockito 测试运行得更快,但您只会拥有使用 @InjectMocks@Mock 显式配置的对象:

@RunWith(MockitoJUnitRunner.class)
public class AutorisationServiceTest {
  
  @InjectMocks
  private IAutorisationService service;
    
  @Mock
  PersonneRepository personneRepository;

请注意,在使用注释和专用运行器时,您永远不必使用 Mockito.mock() 实例化模拟。

我猜你的 PersonneRepository 是 @Service、@Component 或 @Repository。所以你也应该在这里使用自动连线:

@Service
public class AutorisationService implements IAutorisationService {

   @Autowired    
   private PersonneRepository personneRepository;

【讨论】:

    【解决方案2】:

    此答案适用于 testng

    MockitoTestExecutionListener初始化mock,需要使用@MockBean注解。

    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners(MockitoTestExecutionListener.class)
    public class AutorisationServiceTest {   
        @MockBean
        PersonneRepository personneRepository;
    

    另外,不要忘记添加ResetMocksTestExecutionListener 以防止测试间持久模拟(请参阅此issue)。

    【讨论】:

      【解决方案3】:

      您不能同时使用@MockMockito.mock。选择其中一个

      【讨论】:

      • 我删除了 Mockito.mock(PersonneRepository.class),但后来我得到了一个空指针
      • 可以在没有Autowire注解的情况下启动服务吗?
      【解决方案4】:

      在@Before 设置方法中你需要添加MockitoAnnotations.initMocks(this)。这将注入您的模拟服务。我修改了下面的代码:

      @RunWith(SpringJUnit4ClassRunner.class)
          public class AutorisationServiceTest {
          
              @Mock
              PersonneRepository personneRepository;
      
              @InjectMocks
              private AutorisationService service;
          
              @Before
              public void setup() {
                MockitoAnnotations.initMocks(this);
              }
          
          
              @Test
              public void should_Find_GardeWith2Affectations() throws IOException {
          
                  when(personneRepository.getAll(anyString())).thenReturn(DataLoader.mockData());
                  service.getAll("rules");
              }
          
          }
      
          @Service
          public class AutorisationService implements IAutorisationService {
          
              private final PersonneRepository personneRepository;
          
              public AutorisationService(PersonneRepository personneRepository) {
                  this.personneRepository = personneRepository;
              }
      
      @Override
          public List<User> getAll(String where) {
              return personneRepository.getAll(where));
          }
          ...
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多