@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")`
@Testcontainers
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@Sql("/foo.sql")
public class FooControllerTest {
@LocalServerPort private final int port = 8080;
private TestRestTemplate testRestTemplate;
//testcontainers stuff
@BeforeEach
void init() {
testRestTemplate = new TestRestTemplate();
}
@Test
void shouldUpdate(@Autowired FooRepository fooRepository) {
var requestEntity = new HttpEntity<>(fooStuff);
var actual = testRestTemplate.exchange(url,HttpMethod.PUT,requestEntity,FooStuff.class);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Test
void shouldNotUpdate(@Autowired FooRepository fooRepository)
//basically the same test but with invalid request and asserion for HttpStatus.400
}
}
Well, when I run the first test it passes, but when I try to run all of them an exception occurs:
org.springframework.web.client.ResourceAccessException: I/O error on PUT request for "http://localhost:8080/foo": Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
Second test passes without a problem, even if i run all of them