Created
May 20, 2019 17:12
-
-
Save sidneydemoraes/dead8bb8db8bfab5d526e8328758040e to your computer and use it in GitHub Desktop.
@WebMvcTest + Spock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Atentar para o fato que o ByteBuddy pode dar erro. É necessário fazer o upgrade da versão dele de acordo com a versão do Spock utilizado no projeto. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ... | |
.import org.hibernate.PersistentObjectException | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest | |
import org.springframework.boot.test.context.TestConfiguration | |
import org.springframework.context.annotation.Bean | |
import org.springframework.test.web.servlet.MockMvc | |
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | |
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | |
import spock.lang.Specification | |
import spock.mock.DetachedMockFactory | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | |
@WebMvcTest(Controller) | |
@AutoConfigureMockMvc | |
class ControllerIT extends Specification { | |
def "PUT => sucesso" () { | |
given: | |
repository.buscarPorId(_) >> new Entidade(id: 1, status: PAGO) | |
when: | |
def resp = mvc.perform(MockMvcRequestBuilders.put("/entidade/1/proximostatus")) | |
then: | |
resp | |
.andExpect(status().isOk()) | |
.andExpect(MockMvcResultMatchers.jsonPath('status').value('CONCLUIDO')) | |
} | |
def "PUT => id inválido" () { | |
given: | |
def idInvalido = -15 | |
when: | |
def resp = mvc.perform(MockMvcRequestBuilders.put("/entidade/${idInvalido}/proximostatus")) | |
then: | |
resp | |
.andExpect(status().isBadRequest()) | |
.andExpect(content().string('Id inválido')) | |
} | |
def "PUT => id não encontrada" () { | |
given: | |
repository.buscarPorId(_) >> null | |
when: | |
def resp = mvc.perform(MockMvcRequestBuilders.put("/entidade/1/proximostatus")) | |
then: | |
resp | |
.andExpect(status().isNotFound()) | |
} | |
def "PUT => erro na persistência" () { | |
given: | |
repository.buscarPorId(1) >> new Entidade(id: 1, status: PAGO) | |
repository.salvar(_) >> { throw new PersistentObjectException() } | |
when: | |
def resp = mvc.perform(MockMvcRequestBuilders.put("/entidade/1/proximostatus")) | |
then: | |
resp | |
.andExpect(status().isInternalServerError()) | |
} | |
/***********************************************************************/ | |
@Autowired | |
private MockMvc mvc | |
@Autowired | |
private Repository repository | |
@TestConfiguration | |
static class DettachedConfig { | |
DetachedMockFactory detachedMockFactory = new DetachedMockFactory() | |
@Bean | |
Repository repository() { | |
return detachedMockFactory.Stub(Repository) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment