Skip to content

Commit 8e7c736

Browse files
sylverminingjxblum
authored andcommitted
Move groovy test to java
1 parent 1a318b8 commit 8e7c736

File tree

24 files changed

+1003
-549
lines changed

24 files changed

+1003
-549
lines changed

samples/mongo/build.gradle

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ dependencies {
3030

3131
testCompile "org.springframework.boot:spring-boot-starter-test"
3232

33-
integrationTestCompile gebDependencies,
34-
"org.spockframework:spock-spring:$spockVersion"
33+
integrationTestCompile seleniumDependencies
3534

3635
}
3736

3837
integrationTest {
3938
doFirst {
4039
def port = reservePort()
4140

42-
def host = 'localhost:' + port
43-
systemProperties['geb.build.baseUrl'] = 'http://'+host+'/'
44-
systemProperties['geb.build.reportsDir'] = 'build/geb-reports'
4541
systemProperties['server.port'] = port
4642
systemProperties['management.port'] = 0
4743

samples/mongo/src/integration-test/groovy/sample/BootTests.groovy

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import java.util.Set;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.openqa.selenium.By;
26+
import org.openqa.selenium.Cookie;
27+
import org.openqa.selenium.WebDriver;
28+
import org.openqa.selenium.WebElement;
29+
import sample.pages.HomePage;
30+
import sample.pages.LoginPage;
31+
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
34+
import org.springframework.boot.test.context.SpringBootTest;
35+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
36+
import org.springframework.test.context.junit4.SpringRunner;
37+
import org.springframework.test.web.servlet.MockMvc;
38+
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
39+
40+
import static org.assertj.core.api.Assertions.assertThat;
41+
42+
/**
43+
* @author Pool Dolorier
44+
*/
45+
@RunWith(SpringRunner.class)
46+
@AutoConfigureMockMvc
47+
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
48+
public class BootTests {
49+
50+
@Autowired
51+
private MockMvc mockMvc;
52+
53+
private WebDriver driver;
54+
55+
@Before
56+
public void setUp() {
57+
this.driver = MockMvcHtmlUnitDriverBuilder
58+
.mockMvcSetup(this.mockMvc)
59+
.build();
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
this.driver.quit();
65+
}
66+
67+
@Test
68+
public void unauthenticatedUserSentToLogInPage() {
69+
HomePage homePage = HomePage.go(this.driver);
70+
LoginPage loginPage = homePage.unauthenticated();
71+
loginPage.assertAt();
72+
}
73+
74+
@Test
75+
public void logInViewsHomePage() {
76+
LoginPage loginPage = LoginPage.go(this.driver);
77+
loginPage.assertAt();
78+
HomePage homePage = loginPage.login("user", "password");
79+
homePage.assertAt();
80+
WebElement username = homePage.getDriver().findElement(By.id("un"));
81+
assertThat(username.getText()).isEqualTo("user");
82+
Set<Cookie> cookies = homePage.getDriver().manage().getCookies();
83+
assertThat(cookies).extracting("name").contains("SESSION");
84+
assertThat(cookies).extracting("name").doesNotContain("JSESSIONID");
85+
}
86+
87+
@Test
88+
public void logoutSuccess() {
89+
LoginPage loginPage = LoginPage.go(this.driver);
90+
HomePage homePage = loginPage.login("user", "password");
91+
LoginPage successLogoutPage = homePage.logout();
92+
successLogoutPage.assertAt();
93+
}
94+
95+
@Test
96+
public void loggedOutUserSentToLoginPage() {
97+
LoginPage loginPage = LoginPage.go(this.driver);
98+
HomePage homePage = loginPage.login("user", "password");
99+
homePage.logout();
100+
HomePage backHomePage = HomePage.go(this.driver);
101+
LoginPage backLoginPage = backHomePage.unauthenticated();
102+
backLoginPage.assertAt();
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,25 +13,28 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package sample.pages
1716

18-
import geb.*
17+
package sample.pages;
18+
19+
import org.openqa.selenium.WebDriver;
1920

2021
/**
21-
* The Links Page
22-
*
23-
* @author Rob Winch
22+
* @author Pool Dolorier
2423
*/
25-
class LoginPage extends Page {
26-
static url = '/login'
27-
static at = { assert driver.title == 'Login Page'; true}
28-
static content = {
29-
form { $('form') }
30-
submit { $('input[type=submit]') }
31-
login(required:false) { user='user', pass='password' ->
32-
form.username = user
33-
form.password = pass
34-
submit.click(HomePage)
35-
}
24+
public abstract class BasePage {
25+
26+
private WebDriver driver;
27+
28+
public BasePage(WebDriver driver) {
29+
this.driver = driver;
30+
}
31+
32+
public WebDriver getDriver() {
33+
return this.driver;
34+
}
35+
36+
public static void get(WebDriver driver, String get) {
37+
String baseUrl = "http://localhost";
38+
driver.get(baseUrl + get);
3639
}
3740
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import org.openqa.selenium.WebDriver;
20+
import org.openqa.selenium.WebElement;
21+
import org.openqa.selenium.support.FindBy;
22+
import org.openqa.selenium.support.PageFactory;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* @author Pool Dolorier
28+
*/
29+
public class HomePage extends BasePage {
30+
31+
@FindBy(css = "input[type='submit']")
32+
private WebElement submit;
33+
34+
public HomePage(WebDriver driver) {
35+
super(driver);
36+
}
37+
38+
public static HomePage go(WebDriver driver) {
39+
get(driver, "/");
40+
return PageFactory.initElements(driver, HomePage.class);
41+
}
42+
43+
public LoginPage unauthenticated() {
44+
return LoginPage.go(getDriver());
45+
}
46+
47+
public LoginPage logout() {
48+
this.submit.click();
49+
return LoginPage.go(getDriver());
50+
}
51+
52+
public void assertAt() {
53+
assertThat(getDriver().getTitle()).isEqualTo("Spring Session Sample - Secured Content");
54+
}
55+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import org.openqa.selenium.WebDriver;
20+
import org.openqa.selenium.WebElement;
21+
import org.openqa.selenium.support.FindBy;
22+
import org.openqa.selenium.support.PageFactory;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* @author Pool Dolorier
28+
*/
29+
public class LoginPage extends BasePage {
30+
31+
@FindBy(name = "username")
32+
private WebElement username;
33+
34+
@FindBy(name = "password")
35+
private WebElement password;
36+
37+
@FindBy(css = "input[name='submit']")
38+
private WebElement submit;
39+
40+
public LoginPage(WebDriver driver) {
41+
super(driver);
42+
}
43+
44+
public static LoginPage go(WebDriver driver) {
45+
get(driver, "/login");
46+
return PageFactory.initElements(driver, LoginPage.class);
47+
}
48+
49+
public void assertAt() {
50+
assertThat(getDriver().getTitle()).isEqualTo("Login Page");
51+
}
52+
53+
public HomePage login(String user, String password) {
54+
this.username.sendKeys(user);
55+
this.password.sendKeys(password);
56+
this.submit.click();
57+
return HomePage.go(getDriver());
58+
}
59+
}

samples/rest/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ dependencies {
1616
providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion"
1717

1818
testCompile "junit:junit:$junitVersion",
19-
"org.springframework.security:spring-security-test:$springSecurityVersion"
20-
21-
integrationTestCompile spockDependencies,
22-
'org.codehaus.groovy.modules.http-builder:http-builder:0.7'
19+
"org.springframework.security:spring-security-test:$springSecurityVersion",
20+
"org.assertj:assertj-core:$assertjVersion",
21+
"org.springframework:spring-test:$springVersion",
22+
"commons-codec:commons-codec:1.6"
2323
}

0 commit comments

Comments
 (0)