Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added RecycledNinjaServerTester for speedy unit tests
- Loading branch information
Showing
7 changed files
with
196 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...c/site/markdown/documentation/testing_your_application/ninja_recycled_tester.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
RecycledNinjaServerTester | ||
========================= | ||
|
||
Introduction | ||
------------ | ||
|
||
RecycledNinjaServerTester provides much of the same functionality as NinjaTest, | ||
but starts up a single Ninja test server for all tests in a Junit test class. | ||
|
||
If you're unit tests do not rely on a fresh server then this approach will | ||
dramatically increase the speed of your unit tests. | ||
|
||
<pre class="prettyprint"> | ||
public class MyControllerTest extends RecycledNinjaServerTester { | ||
|
||
@Test | ||
public void testThatHomepageWorks() { | ||
NinjaTestBrowser ninjaTestBrowser = new NinjaTestBrowser(); | ||
|
||
// redirect will send a location: redirect in the headers | ||
String result = ninjaTestBrowser.makeRequest(withBaseUrl("/")); | ||
|
||
// If the redirect has worked we must see the following text | ||
// from the index screen: | ||
assertTrue(result.contains("Hello to the blog example!")); | ||
assertTrue(result.contains("My second post")); | ||
} | ||
|
||
} | ||
</pre> | ||
|
||
If you want to revert back to a single Ninja test server for each test method, | ||
you can simply extend your class from FreshNinjaServerTester. | ||
|
||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
ninja-test-utilities/src/main/java/ninja/BaseNinjaServerTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2015 Fizzed Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ninja; | ||
|
||
import com.google.inject.Injector; | ||
import ninja.utils.NinjaTestServer; | ||
|
||
abstract public class BaseNinjaServerTester { | ||
|
||
abstract public NinjaTestServer getNinjaTestServer(); | ||
|
||
public String baseUrl() { | ||
return getNinjaTestServer().getBaseUrl(); | ||
} | ||
|
||
public String withBaseUrl(String path) { | ||
return getNinjaTestServer().getBaseUrl() + path; | ||
} | ||
|
||
public Injector getInjector() { | ||
return getNinjaTestServer().getInjector(); | ||
} | ||
|
||
} |
50 changes: 25 additions & 25 deletions
50
.../main/java/ninja/NinjaDocTester.java.save → ...in/java/ninja/FreshNinjaServerTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
/** | ||
/* | ||
* Copyright (C) 2012-2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ninja; | ||
|
||
import ninja.utils.NinjaTestServer; | ||
|
||
import org.doctester.DocTester; | ||
import org.doctester.testbrowser.Url; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import com.google.inject.Injector; | ||
|
||
/** | ||
* Superclass for doctests that require a running server. Uses {@link NinjaTest} for the server | ||
* stuff. | ||
* Creates a fresh <code>NinjaTestServer</code> for each unit test in your | ||
* junit test class. Unless you really need a fresh server for each unit test, | ||
* please note that its fairly expensive to start/stop a ninja test server | ||
* for each unit test. | ||
* | ||
* @author hschuetz | ||
* <code> | ||
* public class MyControllerTest extends FreshNinjaServerTester { | ||
* | ||
* @Test | ||
* public void usersIndex() { | ||
* String url = withBaseUrl("/users"); | ||
* // do rest of test... | ||
* } | ||
* | ||
* } | ||
* </code> | ||
* | ||
* @see RecycledNinjaServerTester | ||
*/ | ||
public abstract class NinjaDocTester extends DocTester { | ||
public class FreshNinjaServerTester extends BaseNinjaServerTester { | ||
|
||
private NinjaTestServer ninjaTestServer; | ||
|
||
public NinjaDocTester() { | ||
} | ||
|
||
|
||
@Before | ||
public void startServerInTestMode() { | ||
public void startNinjaServer() { | ||
ninjaTestServer = new NinjaTestServer(); | ||
} | ||
|
||
@After | ||
public void shutdownServer() { | ||
public void shutdownNinjaServer() { | ||
ninjaTestServer.shutdown(); | ||
} | ||
|
||
@Override | ||
public Url testServerUrl() { | ||
// v5.1.5+ includes configured context path | ||
return Url.host(ninjaTestServer.getBaseUrl()); | ||
} | ||
|
||
public Injector getInjector() { | ||
return ninjaTestServer.getInjector(); | ||
public NinjaTestServer getNinjaTestServer() { | ||
return ninjaTestServer; | ||
} | ||
|
||
} |
Oops, something went wrong.