https://github.com/commonjava/http-testserver
Simple http server suitable for use with functional tests that shouldn't depend on external URLs
https://github.com/commonjava/http-testserver
Last synced: 5 months ago
JSON representation
Simple http server suitable for use with functional tests that shouldn't depend on external URLs
- Host: GitHub
- URL: https://github.com/commonjava/http-testserver
- Owner: Commonjava
- Created: 2015-07-06T16:19:50.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-06-20T16:24:29.000Z (over 1 year ago)
- Last Synced: 2025-07-07T00:11:31.407Z (5 months ago)
- Language: Java
- Size: 146 KB
- Stars: 0
- Watchers: 7
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simulating remote HTTP servers for functional testing
This is a test fixture, which provides a very basic servlet that registers expected requests and logs access counts for each requested method/pathParts combination. If no expectation is registered for a particular method/pathParts, 404 is returned.
Usage is pretty simple:
#### For junit 4 Rule based
@Rule
public ExpectationServerRule serverRule = new ExpectationServerRule( "repos" );
@Test
public void run()
throws Exception
{
final ExpectationServer server = serverRule.getServer();
final String pathParts = "/repos/pathParts/to/something.txt";
final String content = "this is the content";
final String url = server.formatUrl( pathParts );
server.expect( url, 200, content );
// Do any assertions....
.......
}
#### For junit 5 Extension Based:
@ExtendWith(ExpectationServerExtension.class)
public class ExpectaionTest{
@Expected("repos")
public ExpectationServer server;
@Test
public void run()
throws Exception
{
final String pathParts = "/repos/pathParts/to/something.txt";
final String content = "this is the content";
final String url = server.formatUrl( pathParts );
server.expect( url, 200, content );
// Do any assertions....
.......
}
}
or:
public class ExpectaionTest{
@RegisterExtension
public ExpectationServerExtension extension = new ExpectationServerExtension("repos");
@Test
public void run()
throws Exception
{
final ExpectationServer server = extension.getServer();
final String pathParts = "/repos/pathParts/to/something.txt";
final String content = "this is the content";
final String url = server.formatUrl( pathParts );
server.expect( url, 200, content );
// Do any assertions....
.......
}
}
#### Quarkus Based Test
There are some limitations to let junit5 @ExtendWith work together with @QuarkusTest, see https://github.com/quarkusio/quarkus/issues/24911#issuecomment-1098935690
So to make it work, here brings the new annotation to make it work.
@QuarkusTest
public class ExpectaionTest{
@InjectExpected("repos")
ExpectationServer server;
@Test
public void run()
throws Exception
{
final String pathParts = "/repos/pathParts/to/something.txt";
final String content = "this is the content";
final String url = server.formatUrl( pathParts );
server.expect( url, 200, content );
// Do any assertions....
.......
}
}