Sprint TestRestTemplate返回Id null

时间:2016-04-16 12:30:30

标签: java spring rest spring-boot integration-testing

我有一个Spring应用程序的集成测试,它使用TestRestTemplate发出请求。每次我发出请求时,模板都会返回一个实体,但id始终为null。如果我通过Postman做同样的事情,那么id不是null,所以我必须与测试本身有关,但我不知道它是什么。有没有人有想法?

测试看起来像这样:

 <ul id="filter" class="option-set clearfix">
                            <li data-filter="*" class="selected"><a>All</a></li>
                            <asp:Repeater ID="rpYear" runat="server">
                                <ItemTemplate>           

                                     <li data-filter=".<%#Eval("Year") %>"><a href="#"><%#Eval("Year") %></a></li>

                                </ItemTemplate>
                            </asp:Repeater>
                        </ul>

API端点如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest(randomPort = true)
public class GameServiceControllerIT {
    @Value("${local.server.port}")
    private int port;

    private URL base;
    private RestTemplate template;

    @Before
    public void setUp()
            throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
        this.template = new TestRestTemplate();
    }

    @Test
    public void testAddGame() {
        User user = addUser();
        ResponseEntity<Game> gameEntity = template.exchange(base + "/games/new?token=" + user.getToken(), HttpMethod.POST, null, Game.class);

        Assert.assertThat(gameEntity.getStatusCode(), is(HttpStatus.OK));               //This works
        Assert.assertThat(gameEntity.getBody().getOwner(), is(user.getUsername()));     //This works too
        Assert.assertThat(gameEntity.getBody().getId(), is(not(null)));                 //This doesn't work
    }

1 个答案:

答案 0 :(得分:0)

我发现了同样的问题。不知何故,“Id”字段对于RestTemplate有一些含义,因此它变为“保留”。如果您有权访问生成响应的服务器,只需将字段名称更改为“Iid”,例如,在Game类中。必须将getter getId()重命名为getIid()。