thetruth
thetruth

Reputation: 15

Can not Autowire bean in Junit

I am trying to unit test a Controller Class with Junit. However, when I try to autowire my PlayerRepository interface, which extends crudRepository, it gives this error:

2018-12-06 21:59:39.530 ERROR 8780 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@78e117e3] to prepare test instance [edu.ceng.gameproject.player.PlayerControllerTest@4f704591]

(I did not put the entire error since it is very long.)

and it also says:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'edu.ceng.gameproject.player.PlayerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

By the way, I can do the autowire in my controller to make changes on database. It just does not work in testing. Here is my code:

Controller Class:

@Controller    // This means that this class is a Controller
@RequestMapping(path="/Player") // This means URL's start with /Player 
(after Application path)

public class PlayerController {

 @Autowired 
 private PlayerRepository playerRepository;
}

Here is the PlayerRepsitory interface:

@Repository
public interface PlayerRepository extends CrudRepository<Player, String> {


}

Abstract test class where I do the autowiring :

 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest(classes = Main.class)

 @WebAppConfiguration
 public abstract class GameProjectBackEndApplicationTests {

 protected MockMvc mvc;

 @Autowired
 WebApplicationContext webApplicationContext;

 @Autowired
 PlayerRepository playerRepository;


 protected void setUp() {
     mvc = 
  MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
 }

}

PlayerControllerTest class where I use the autowired playerRepository:

public class PlayerControllerTest extends GameProjectBackEndApplicationTests 
{

    @Override
    @Before
    public void setUp() {
        super.setUp();
    }

    @Test
    public void test_getUsersList_withOk() throws Exception {
        String uri = "/Player/all";

        // Create user in the database
        Player createdUser = playerRepository.save(new Player("testUser", 
"testPassword"));

        MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri)
                .accept(MediaType.APPLICATION_JSON_VALUE)).andReturn();

        // check if status is 200 - OK
        int status = mvcResult.getResponse().getStatus();
        assertEquals(200, status);

        String content = mvcResult.getResponse().getContentAsString();
        Player[] playerList = super.mapFromJson(content, Player[].class);

        // check if list has actually any user
        assertTrue(playerList.length > 0);


        // check returned list has user that we created
        boolean contains = false;
        for (int i = 0; i < playerList.length; i++) {
            if 
              (createdUser.getUsername().equals(playerList[i].getUsername())
                    && 
    createdUser.getPasswd().equals(playerList[i].getPasswd())) {
                contains = true;
            }
        }
        // assert there is a user that we created
        assertTrue(contains);
        //delete created user
        playerRepository.deleteById(createdUser.getUsername());
    }
}

Thanks in advance.

Upvotes: 1

Views: 2707

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

As I said in the comments, use @MockBean to inject a mock for every dependency needed in your controller. Your test class will look like this.

 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = Main.class)

 @WebAppConfiguration
 public class GameProjectBackEndApplicationTests {

     private MockMvc mvc;

     @Autowired
     private WebApplicationContext webApplicationContext;

     @MockBean
     private PlayerRepository playerRepository;

     @Before
     public void setUp() {
         mvc = 
         MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

         // Mock calls to PlayerRepository
         // Mockito.when(playerRepository.getEntries(1)).thenReturn(myList);
     }

     @Test
     public void myTest() {
      ....
     }

}

Also, I don't really recommend using inheritance for your tests. It is better to have everything in one place.

Upvotes: 1

Related Questions