我已经使用JUnit 4和spring-test库编写了一些JUnit测试.当我在Eclipse中运行测试然后运行正常并通过.但是当我使用Maven运行它们时(在构建过程中),它们无法给出与弹簧相关的错误.我不确定导致问题的是什么,JUnit,Surefire或Spring.这是我的测试代码,弹簧配置和我从Maven获得的异常:
PersonServiceTest.java
package com.xyz.person.test;
import static com.xyz.person.util.FjUtil.toFjList;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.xyz.person.bo.Person;
import com.xyz.person.bs.PersonService;
import fj.Effect;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:personservice-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class PersonServiceTest {
@Autowired
private PersonService service;
@Test
@Transactional
public void testCreatePerson() {
Person person = new Person();
person.setName("abhinav");
service.createPerson(person);
assertNotNull(person.getId());
}
@Test
@Transactional
public void testFindPersons() {
Person …Run Code Online (Sandbox Code Playgroud) 更新02/05/2018(大约4年后)...我再次对此进行了测试,因为人们一直在讨论我的问题/答案,Sotirios Delimanolis是正确的,我不应该在我的答案中编写代码来完成这项工作.我使用了基本相同的RestTemplate/REST服务设置,如我的问题所示,REST服务具有确认的响应内容类型application/json,RestTemplate能够处理响应而没有问题进入Map.
我正在调用一个JSON像这样返回的休息服务:
{
"some.key" : "some value",
"another.key" : "another value"
}
Run Code Online (Sandbox Code Playgroud)
我想我可以使用java.util.Map响应类型来调用此服务,但这对我不起作用.我得到这个例外:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map]
Run Code Online (Sandbox Code Playgroud)
我应该只指定String为响应类型并将其转换JSON为Map?
编辑我
这是我的restTemplate调用:
private Map<String, String> getBuildInfo(String buildUrl) {
return restTemplate.getForObject(buildUrl, Map.class);
}
Run Code Online (Sandbox Code Playgroud)
这是我如何设置restTemplate:
@PostConstruct
public void initialize() {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = …Run Code Online (Sandbox Code Playgroud) 我们有一个休息服务,它在类型为 的映射中返回一个字节数组。如果我在没有泛型的情况下使用 Map,则在接收响应时,字节数组数据将转换为字符串。是否可以仅从服务器发送字节数据,如果可以,如何使用 RestTemplate 从客户端检索该数据?
ResponseEntity<Map<String, byte[]>> result result = restTemplate.exchange("http://localhost:8085/api/fetchContent?Id=" + contentId+"&userName=trump", HttpMethod.GET, entity, Map.class, params);
Run Code Online (Sandbox Code Playgroud)
上面的代码将给出一个编译问题,因为返回类型是地图。
java ×2
resttemplate ×2
spring ×2
json ×1
maven-2 ×1
rest ×1
spring-boot ×1
spring-test ×1
surefire ×1