Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.3">
<meta name="author" content="Dennis Espen">
<title>/api/offers Getting Started Guide</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
@espendennis
espendennis / OfferApiTests.java
Created October 18, 2016 13:51
Initial setup OfferApiTests
package com.espen.tests;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@espendennis
espendennis / HomeControllerTests.java
Created October 18, 2016 13:36
Testing formdata
@Test
public void testNewOffer() throws Exception {
Offer testoffer = new Offer("München", "Bozen", "user1", "2016-10-11T08:50", 100);
mockMvc.perform(post("/newoffer").with(user("admin").password("123456seven"))
.param("startingPoint", testoffer.getStartingPoint())
.param("destination", testoffer.getDestination())
.param("username", testoffer.getUsername())
.param("date", testoffer.getDate())
.param("price", new Double(testoffer.getPrice()).toString())
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
@espendennis
espendennis / HomeControllerTests.java
Created October 18, 2016 13:34
Testing Logging in
@Test
public void testLoggingIn() throws Exception {
mockMvc.perform(formLogin().user("admin").password("123456seven")).andExpect(authenticated());
}
@Test
public void testLogginFail() throws Exception {
mockMvc.perform(formLogin().user("notExistent").password("asdasdad")).andExpect(unauthenticated());
}
@espendennis
espendennis / HomeControllerTests.java
Created October 18, 2016 13:33
Checking the model
@Test
public void testShowOffer() throws Exception {
mockMvc.perform(get("/offer/{id}", offer1.getId())).andExpect(status().isOk()).andExpect(view().name("offer"))
.andExpect(forwardedUrl("/WEB-INF/tiles/templates/baselayout.jsp"))
.andExpect(model().attribute("offer", hasProperty("id", is(offer1.getId()))))
.andExpect(model().attribute("offer", hasProperty("startingPoint", is(offer1.getStartingPoint()))))
.andExpect(model().attribute("offer", hasProperty("destination", is(offer1.getDestination()))))
.andExpect(model().attribute("offer", hasProperty("username", is(offer1.getUsername()))))
.andExpect(model().attribute("offer", hasProperty("date", is(offer1.getDate()))));
}
@espendennis
espendennis / HomeControllerTests.java
Created October 18, 2016 13:30
Tests with login
@Test
public void testOfferCreated() throws Exception {
mockMvc.perform(get("/offercreated").with(user("admin").password("123456seven"))).andExpect(status().isOk())
.andExpect(view().name("offercreated"))
.andExpect(forwardedUrl("/WEB-INF/tiles/templates/baselayout.jsp"));
}
@Test
public void testHome() throws Exception {
mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("home"))
.andExpect(forwardedUrl("/WEB-INF/tiles/templates/baselayout.jsp")).andExpect(model().size(1));
}
@espendennis
espendennis / HomeControllerTests.java
Created October 18, 2016 13:21
Start HomeControllerTests
package com.espen.tests;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.TestPropertySource;
@espendennis
espendennis / OfferApiTests.java
Created October 18, 2016 13:14
Completed Test
@Test
public void testPutOffer() throws Exception {
ArrayList<Offer> offers = (ArrayList<Offer>) offersService.findAll();
Offer offerToChange = offers.get(4);
offerToChange.setStartingPoint("München");
offerToChange.setDestination("Frankfurt");
offerToChange.setPrice(100);
offerToChange.setUsername("admin");
offerToChange.setDate("2017-11-11T09:25");
String json = mapper.writeValueAsString(offerToChange);
@Test
public void testPutOffer() throws Exception {
ArrayList<Offer> offers = (ArrayList<Offer>) offersService.findAll();
Offer offerToChange = offers.get(4);
offerToChange.setStartingPoint("München");
offerToChange.setDestination("Frankfurt");
offerToChange.setPrice(100);
offerToChange.setUsername("admin");
offerToChange.setDate("2017-11-11T09:25");
String json = mapper.writeValueAsString(offerToChange);