playframework / playframework/play1
Data not queryable during functional test causing failures
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.6k
- Forks
- 671
- Avg merge
- 12d 15h
- Merged PRs (30d)
- 1
Description
Are you looking for help?
Yes, I've tried asking the question on SO, but seems not a lot of people are keen to help. Hoping that the developers closer to the code will understand the problem.
For reference, the question is here https://stackoverflow.com/questions/66466318/play-framework-functional-test-ghost-data
Play Version (1.5.x / etc)
Play Version 1.5.4
Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)
Darwin rdpa-macbookpro 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64
JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Description
Hoping someone else is having the same issue as me, or has other ideas.
I'm currently running Play 1.4.x, but also working on upgrading to play 1.5.x, though I verified the same issue happens on both versions. I created a simple Functional Test that loads data via fixtures:
My fixture for loading test data is like so
data.yml
User(testUser):
name: blah
AccessToken(accessToken):
user: testUser
token: foo
Data(testData):
user: testUser
...
I've created a controller to do something with the data like this, that has middleware for authentication check. The routes file will map something like /foo to BasicController.test
public class BasicController extends Controller{
@Before
public void doAuth(){
String token = "foo"; // Get token somehow from header
AccessToken token = AccessToken.find("token = ?", token).first(); // returns null;
// do something with the token
if(token == null){
//return 401
}
//continue to test()
}
public void test(){
User user = //assured to be logged-in user
... // other stuff not important
}
}
Finally I have my functional test like so:
public class BasicControllerTest extends FunctionalTest{
@org.junit.Before
public void loadFixtures(){
Fixtures.loadModels("data.yml");
}
@Test
public void doTest(){
Http.Request request = newRequest()
request.headers.put(...); // Add auth token to header
AccessToken.findAll().stream().forEach(a -> Logger.info("Token : " + a.token); // Prints out the tokens that exist successfully
Http.Response response = GET(request, "/foo");
assertIsOk(response);
}
}
Now, the problem I'm running into, is that I can verify the token is still visible in the headers, but running the line
AccessToken token = AccessToken.find("token = ?", token).first(); returns null
I verified in the functional test, before calling the GET method that the accessToken and user were created successfully from loading the fixtures. I can see the data in my, H2 in-memory database, through plays new DBBrowser Plugin in 1.5.x. But for some reason the data is not returned in the controller method.
Things I've tried
- Ensuring that the fixtures are loaded only once so there is no race condition where data is cleared while reading it.
- Using multiple ways of querying the database via nativeQuery jpql/hql query language and through plays native way of querying data.
- Testing on different versions of play
Any help would be very much appreciated!
Expected Behavior
Maybe it's a misunderstanding of how functional tests works, but my assumption is that the in-memory H2 database that I can view from localhost:9000/@db and the data that I query from inside of the functional test is the same data that I am querying from inside of the controller.
Actual Behavior
Inside of the controller the query returns no results
Reproducible Test Case
Hopefully the example above will suffice 🤞
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with BasicControllerTest.loadFixtures and data.yml, then trace the GET(request, "/foo") path into BasicController.doAuth. Compare the database context used by Fixtures.loadModels with the controller query and reproduce the null result; done means the cause of the differing query results is identified and documented or fixed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend, databases, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100