java.lang.AssertionError: Expecting actual not to be null
Создал рест апи приложение и теперь начинаю учить тестирование, выдает ошибку на этот тест
import com.product.controller.ProductController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = ProductController.class)
public class ProductControllerTest {
@Autowired
private ProductController productController;
@Test
public void test() throws Exception{
(20 строка) assertThat(productController).isNotNull();
}
}
все сделал как говорится на сайте spring.io, но пишет что тест провален
controller
@RestController
@RequestMapping(value = "/product/")
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping(value = "",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<List<Product>> getAllProducts(){
List<Product> products = this.productService.getAll();
if (products.isEmpty()){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(products,HttpStatus.OK);
}
}
pom
<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
сервис класс
public interface ProductService {
Product getById(BigInteger _id);
void save(Product product);
List<Product> getAll();
List<Long> getAllCategoryIds();
}
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductRepository productRepository;
@Autowired
CategoryIdRepository categoryIdRepository;
@Override
public Product getById(BigInteger _id) {
return productRepository.findOne(_id);
}
@Override
public void save(Product product) {
productRepository.save(product);
}
@Override
public List<Product> getAll(){
return productRepository.findAll();
}
@Override
public List<Long> getAllCategoryIds() {
return categoryIdRepository.findAllId();
}
}
Ответы (1 шт):
Автор решения: petrovich
→ Ссылка
Добавь эту аннотацию над тестовым классом, должно помочь
@RunWith(SpringRunner.class)