title: Unit Test Java API/Http requests
body: <p>I've have built a Java API that at the moment can add an entry to a H2 Database and retrieve all entries. I've build the app based on this tutorial: <a href="http://www.springboottutorial.com/spring-boot-crud-rest-service-with-jpa-hibernate" rel="nofollow noreferrer">http://www.springboottutorial.com/spring-boot-crud-rest-service-with-jpa-hibernate</a></p>
  
  <p>This is the code that I have written:</p>
  
  <p>Offer.java</p>
  
  <pre><code>package com.java.api.offers.SpringBootOffers.Offer;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  
  @Entity
  public class Offer {
      @Id
      @GeneratedValue
  
  private int id;
  private String description;
  private int price;
  
  public Offer(){
      super();
  }
  
  public Offer(int id, String description, int price) {
      super();
      this.id = id;
      this.description = description;
      this.price = price;
  }
  
  public int getId(){
      return id;
  }
  
  public void setId(int id){
      this.id = id;
  }
  
  public String getDescription(){
      return description;
  }
  
  public void setDescription(String desc){
      this.description = desc;
  }
  
  public int getPrice(){
      return price;
  }
  
  public void setPrice(int price){
      this.price = price;
  }
  }
  </code></pre>
  
  <p>OfferController.java</p>
  
  <pre><code>package com.java.api.offers.SpringBootOffers.Offer;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
  
  import java.net.URI;
  import java.util.List;
  
  @RestController
  public class OfferController {
  
  @Autowired
  private OfferRepository offerRepository;
  
  @GetMapping("/offers")
  public List&lt;Offer&gt; getAllOffers() {
      return offerRepository.findAll();
  }
  
  
  @PostMapping("/offers")
  public ResponseEntity&lt;Object&gt; createStudent(@RequestBody Offer offer) {
      Offer savedOffer = offerRepository.save(offer);
  
      URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
              .buildAndExpand(savedOffer.getId()).toUri();
  
      return ResponseEntity.created(location).build();
  
  }
  
  }
  </code></pre>
  
  <p>OfferRepository.java</p>
  
  <pre><code>package com.java.api.offers.SpringBootOffers.Offer;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface OfferRepository extends JpaRepository&lt;Offer, Long&gt; {};
  </code></pre>
  
  <p>And I am trying to now unit test the GET and POST method, however I'm unsure as to how it works. I have written the following test:</p>
  
  <pre><code>package com.java.api.offers.SpringBootOffers;
  
  import com.java.api.offers.SpringBootOffers.Offer.Offer;
  import com.java.api.offers.SpringBootOffers.Offer.OfferController;
  import com.java.api.offers.SpringBootOffers.Offer.OfferRepository;
  import org.apache.http.HttpResponse;
  import org.apache.http.HttpStatus;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpUriRequest;
  import org.apache.http.impl.client.HttpClientBuilder;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.assertj.core.api.Assertions;
  import org.springframework.beans.factory.annotation.Autowired;
  
  
  
  import java.io.IOException;
  
  @RunWith(SpringRunner.class)
  @WebMvcTest(value = OfferController.class)
  public class SpringBootOffersApplicationTests {
  
      @Autowired
      private OfferRepository offerRepository;
  
      @Test
      public void getAllOffers() throws ClientProtocolException, IOException {
  
          Offer offer = new Offer();
          HttpUriRequest request = new HttpGet("http://localhost:8080/offers");
  
          HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
  
          Assertions.assertThat(httpResponse.getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SC_NOT_FOUND);
      }
  
  }
  </code></pre>
  
  <p>which is failing with following error message:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
      at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
      at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'offerController': Unsatisfied dependency expressed through field 'offerRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.java.api.offers.SpringBootOffers.Offer.OfferRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
      at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:139)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
      ... 25 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.java.api.offers.SpringBootOffers.Offer.OfferRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
      ... 43 more
  </code></pre>
  
  <p>I'm rather new to Java so still trying to figure out how to test the API, so could anyone help with the above error message, or help in general with how to correctly test http methods in java.</p>
  
  <p>Thanks!</p>