Тест Junit всегда возвращает false - PullRequest
0 голосов
/ 22 октября 2019

У меня есть служба, которая проверяет, существует ли продукт. Возвращает true, если найдено, иначе false. Но когда я создаю класс модульного теста и вызываю метод, он всегда возвращает false, даже если продукт существует. Когда я запускаю приложение и тестирую его с помощью почтальона, оно работает как положено, возвращая true, если найдено, иначе false.

Класс обслуживания (работает, как и ожидалось, когда я использую api с использованием почтальона через контроллер)

@Service
@Transactional
public class ProductService {

    private ProductRepository productRepository;

    @Autowired
    public ProductService(ProductRepository productRepository) {this.productRepository = productRepository;}

    //check if a product exists
    public boolean checkIfProductExists(String name)
    {
        Product product =  productRepository.findByName(name);
        if(product != null)
        {
            return true;
        }
        return false;
    }

}

Тест

 @RunWith(SpringRunner.class)
public class ProductServiceTests {

    @MockBean
    private ProductService productService;

    @Test
    public void checkIfProductExistsTest()
    {  
       //result is supposed to be true because ARVs exists
       //when i consume api using postman it returns true, but here false
       //why is it always false no matter the input
       boolean result = productService.checkIfProductExists("ARVs");

       //therefore test fails but it is supposed to pass
       assertEquals(true,result);
    }
}

Ответы [ 5 ]

3 голосов
/ 22 октября 2019

Вам нужно смоделировать ответ для productRepository

when(productRepository.findByName("ARVs")).thenReturn(myProduct);
2 голосов
/ 22 октября 2019

Вы юнит тестируете ProductService и вы издеваетесь над тем же. Это не то, как работает модульное тестирование.

Вы высмеиваете зависимости ProductService, в данном случае ProductRepository.

Итак, ваш тест должен выглядеть как

@RunWith(SpringRunner.class)
public class ProductServiceTest {

    @MockBean
    private ProductRepository repository;

    @Autowired
    private ProductService productService;

    @Test
    public void test() {

        Mockito.when(repository.findProductByName(Mockito.anyString())).thenReturn(new Product());

        boolean result = productService.checkIfProductExists("some product name");

        assertEquals(true, result);
    }

    @TestConfiguration
    static class TestConfig {

        @Bean
        public ProductService productService(final ProductRepository repository) {

        return new ProductService(repository);
        }
    }
}
2 голосов
/ 22 октября 2019

Насмешка ProductService всегда приведет к false. То, что вы должны сделать, это насмешить ProductRepository и ввести его в ProductService. Кроме того, если вы хотите воспроизвести ситуацию, в которой существуют «АРВ», вы должны проинструктировать свой макет.

@RunWith(SpringRunner.class)
public class ProductServiceTests {

    @MockBean
    private ProductRepository productRepository;

    @Autowired
    private ProductService productService;

    @Test
    public void checkIfProductExistsTest()
    {  
       Product myProduct = new Product();
     Mockito.when(productRepository.findByName("ARVs")).thenReturn(myProduct);
       boolean result = productService.checkIfProductExists("ARVs");

       assertEquals(true,result);
    }
}

Если вы не скажете максимизированному объекту, как вести себя, он всегда вернет вамnull для объектов и false для boolean переменных.

1 голос
/ 22 октября 2019

Я думаю, вы бы искали что-то вроде этого для тестового класса:

@RunWith(SpringRunner.class)
public class ProductServiceTests {

    @Autoired
    private ProductService productService;

    @MockBean ProductRepository productRepository;

    @Test
    public void checkTrueIfProductExists()
    {
        Product product = new Product();
        Mockito.when(productRepository.findByName(Mockito.anyString())).thenReturn(product)
        boolean result = productService.checkIfProductExists("foo");

        assertTrue(result);
    }



    @Test
    public void checkFalseIfProductDoesntExists()
    {
        Mockito.when(productRepository.findByName(Mockito.anyString())).thenReturn(null)
        boolean result = productService.checkIfProductExists("bar");

        assertFalse(result);
    }
}

То, что вы ранее объявляли своим ProductService как макет, где на самом деле мы хотели подключить это, а затемиздеваться над хранилищем. Это позволяет нам контролировать результаты, которые возвращает хранилище, используя что-то вроде Mockito.

1 голос
/ 22 октября 2019

Этот вид теста не имеет смысла. Я полагаю, что вы хотели проверить код ProductService, но затем вы создаете для него макет Mockito, который по умолчанию будет возвращать false при вызове checkIfProductxists с любым параметром.

Есливаше намерение - проверить ProductService - это не может быть насмешкой (используйте @Autowired вместо пробного боба). Возможно, вы захотите подумать о том, чтобы смоделировать хранилище, чтобы избежать вызовов БД.

Поэтому вам следует @MockBean ProductRepository productRepository; и затем зарегистрировать ожидания для него:

@RunWith(SpringRunner.class) // you might need to supply some kind of context configuration here as well so that spring will know what beans should be really loaded
public class ProductServiceTest {


 @Autowired ProductService productService;

 @MockBean ProductRepository productRepo;

 @Test
 public void checkIfProductExistsTest() {


  when(productRepo.findByName("ARVs")).thenReturn(<some_product_instance>);

  boolean result = productService.checkIfProductExists("ARVs");
  assertTrue(result);
 }

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...