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!

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 Testing With Our Test Context

HttpClientErrorException: 403 Forbidden

All of my test fail when i try to run my test at 6:10 in the video, and i get this error: HttpClientErrorException: 403 Forbidden

Here is my code:

package com.teamtreehouse.service;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.closeTo;

import com.teamtreehouse.config.AppConfig; import com.teamtreehouse.service.dto.geocoding.Location; import com.teamtreehouse.service.dto.weather.Weather; import com.teamtreehouse.service.resttemplate.weather.WeatherServiceImpl; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.client.RestTemplate;

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class WeatherServiceTest {

@Autowired private WeatherService service;

private Location loc; private Weather weather;

private static final double ERROR_GEO = 0.0000001;

@Before public void setup(){ loc = new Location(41.881832,-87.623177); weather = service.findByLocation(loc); }

@Test public void findByLocation_ShouldReturnSameCoords() throws Exception{ assertThat(weather.getLatitude(),closeTo(loc.getLatitude(),ERROR_GEO)); assertThat(weather.getLongitude(),closeTo(loc.getLongitude(),ERROR_GEO)); }

@Test public void findByLocation_ShouldReturn8DaysForecastData() throws Exception{


@Test public void findByLocation_ShouldReturnCurrentConditions() throws Exception{


@Configuration @PropertySource("api.properties") public static class TestConfig{ @Autowired Environment env;

public RestTemplate restTemplate(){
  return AppConfig.defaultRestTemplate();

public WeatherService weatherService(){
  WeatherService service = new WeatherServiceImpl(
  return service;

} }

3 Answers

Luis Mansilla
Luis Mansilla
4,539 Points

When you have HTTP errors, you must check the URI in the code and configuration is OK first (that exist and that you can access that URI)

If you are using the git checkout command, you will also need to check api.properties to make sure your API keys are still populated.

I made this same mistake, but everything passes as soon as you update this file.

Bakhtiyar Seidakhmetov
Bakhtiyar Seidakhmetov
11,936 Points

99.9% in api.properties file weather.api.key, geocode.api.key and places.api.key fields are empty.

Hope it help someone!