Skip to content

Instantly share code, notes, and snippets.

@MrSnyder
Last active May 25, 2020 10:44
Show Gist options
  • Save MrSnyder/08b71d177979bb1011c12ff980bb4997 to your computer and use it in GitHub Desktop.
Save MrSnyder/08b71d177979bb1011c12ff980bb4997 to your computer and use it in GitHub Desktop.
MyStuff Backend Challenge 2

Challenge: Integration Tests mit Java Spring Boot für REST-basierte CRUD Anwendung

Aufgabe

  • Integration Tests für alle Endpoints bereitstellen
  • Bereitstellung des Codes in eigenem GitHub-Repo

Links

REST-Endpoints

Method Endpoint / example request Purpose Response code
GET /items Retrieve all item resources 200 (OK)
GET /items/4711 Retrieve item resource 4711 200 (OK) / 404 (NOT FOUND)
POST /items Create a new item resource 201 (Created)
PUT /items/4711 Update item resource 4711 200 (OK) / 404 (NOT FOUND)
DELETE /items/4711 Delete item resource 4711 204 (No Content) / 404 (NOT FOUND)

Grundlegender Aufbau wie im Live-Coding demonstriert

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class ItemRestControllerTest {

	private static final String BASE_PATH = "/api/v1/items";

	@Autowired
	private TestRestTemplate restTemplate;

	@Autowired
	private ItemRepository repo;

	@BeforeEach
	void setupRepo() {
		repo.deleteAll();
	}

	@Test
	void shouldBeAbleToUploadAnItem() {
                fail();
	}
	
	@Test
	void shouldReadAllItems() {
		fail();
	}

	@Test
	void shouldFindOneItem() {
		fail();
	}

	@Test
	void shouldFindNoItemForUnknownId() throws URISyntaxException {
		fail();
	}

	@Test
	void shouldBeAbleToDeleteAnItem() throws URISyntaxException {
		fail();
	}

	@Test
	void shouldNotBeAbleToDeleteAnItemWithUnknownId() throws URISyntaxException {
		fail();
	}

	@Test
	void shouldBeAbleToReplaceAnItem() throws URISyntaxException {
		fail();
	}

	@Test
	void shouldNotBeAbleToReplaceAnItemWithUnknownId() throws URISyntaxException {
		fail();
	}

	private ResponseEntity<Item> givenAnInsertedItem() {
		Item item = buildLawnMower();
		return restTemplate.postForEntity(BASE_PATH, item, Item.class);
	}

	private Item buildLawnMower() {
		Item item = Item.builder().name("Lawn mower").amount(1).lastUsed(Date.valueOf("2019-05-01"))
				.location("Basement").build();
		return item;
	}

	private Item buildLawnTrimmer() {
		Item item = Item.builder().name("Lawn trimmer").amount(1).lastUsed(Date.valueOf("2018-05-01"))
				.location("Basement").build();
		return item;
	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment