package br.com.alura.adopet.controller;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import br.com.alura.adopet.api.AdopetApiApplication;
import br.com.alura.adopet.api.service.AdocaoService;
@SpringBootTest(classes = AdopetApiApplication.class)
@AutoConfigureMockMvc
public class AdocaoControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private AdocaoService adocaoService;
@Test
void solicitar_WithInvalidData_ShouldReturnCode400() throws Exception {
//Arrange
String json = "{}";
//Act
var response = mvc.perform(MockMvcRequestBuilders
.post("/adocoes")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
).andReturn().getResponse();
//Assert
assertEquals(400, response.getStatus());
}
}
Subi o mysql no Docker:
version: '3.8'
services:
mysql:
image: mysql:8.0
container_name: mysql-adopet
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: adopet
MYSQL_USER: root
MYSQL_PASSWORD: root
ports:
- "3306:3306"
application.properties:
spring.datasource.url=jdbc:mysql://localhost/adopet?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Erro: