-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
{to,from}Json
in MVC tests. (#490)
This shorthand intends to eliminate some of the clumsy and repetitive interaction with `ObjectMapper` and `MvcResult`.
- Loading branch information
Showing
7 changed files
with
151 additions
and
233 deletions.
There are no files selected for viewing
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
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
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
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
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
32 changes: 32 additions & 0 deletions
32
service/src/test/java/org/databiosphere/workspacedataservice/controller/MockMvcTestBase.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.databiosphere.workspacedataservice.controller; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UnsupportedEncodingException; | ||
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.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest | ||
class MockMvcTestBase { | ||
@Autowired private ObjectMapper mapper; | ||
@Autowired protected MockMvc mockMvc; | ||
|
||
protected String toJson(Object value) throws JsonProcessingException { | ||
return mapper.writeValueAsString(value); | ||
} | ||
|
||
protected <T> T fromJson(MvcResult result, Class<T> valueType) | ||
throws UnsupportedEncodingException, JsonProcessingException { | ||
return mapper.readValue(result.getResponse().getContentAsString(), valueType); | ||
} | ||
|
||
protected <T> T fromJson(InputStream inputStream, Class<T> valueType) throws IOException { | ||
return mapper.readValue(inputStream, valueType); | ||
} | ||
} |
Oops, something went wrong.