Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Functional Testing

Bakhtiyar Seidakhmetov
Bakhtiyar Seidakhmetov
11,936 Points

REST API. Functional testing

Failed testing coursesCanBeAccessedById() test. My code:

@Test

public void coursesCanBeAccessedById() throws Exception {

Course course = new Course("Test", "http://test.com");

courseDao.add(course);

ApiResponse res = client.request("GET",
    "/courses/" + course.getId());
Course retrieved = gson.fromJson(res.getBody(), Course.class);

assertEquals(course, retrieved);

}

While debugging found out that retrieved is NULL. That's seems like gson can't grab data from response. Code is similar to Craigs. What is wrong? HELP pls!!!

Full project here https://github.com/bakhityar/course-review-test

2 Answers

Boban Talevski
Boban Talevski
24,793 Points

Hi Bakhtiyar, not sure if you maybe sorted this already. The code posted here seems ok. In fact, I plugged in your whole ApiTest class from github inside my project and the test passes. So the problem is somewhere else, possibly in the ApiCllient? Also, is everything working as expected if you do manual testing for the same functionality?

If you could post your full project on github I could try and be of more help. I see you have it as separate repositories for the project and the test part which makes it harder to copy your project as a whole and test it on my machine as an exact copy of what you have. That way I would probably experience the same issue and it'll be easier to troubleshoot and help.

Or you can check my github repo for this project and see if you can maybe compare stuff and troubleshoot that way https://github.com/edison696/course-reviews.

UPDATE: Ah, I think I found it :). It's probably this line in your Api.java

    Sql2o sql2o = new Sql2o(
        String.format("%s,;INIT=RUNSCRIPT from 'classpath:db/init.sql'", datasource)
        ,"","");

Notice how you have a comma right after the placeholder %s, which is put in the actual String and messes up the syntax. This way, you are not getting the database connection at all, whether you use the testing or the "production" one, i.e. with or without the arguments passed to the Main method.