mock bean unable to inject dependecy in unit test

LowCool

I want to test my spring boot application using TestRestemplate, however I am unable to test get url, what I am trying to do here is I am injecting some predefine objects setting into list and mocking find all method.

below is my test code looks like.

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class PersonControllerTests {

    
    @Autowired
    private TestRestTemplate restTemplate;
    
    @MockBean
    private PersonRepository mockRepository;
    
    @Before
    public void init() {
        List<Person> list = new ArrayList<>();
        Person p1 = new Person("dumm1", "lastName1",22);
        Person p2 = new Person("dumm2", "lastName2",32);
        p1.setId(1l);
        list.add(p2);
        list.add(p1);
        when(mockRepository.findAll()).thenReturn(list);
        
    }

    @Test
    public void getPersonsGoodReq() throws Exception {
    
    ResponseEntity<Person[]> response = restTemplate
            .withBasicAuth("admin", "password")
            .getForEntity("/persons/all", Person[].class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(response.getBody().length, 2);
    }
    
}

I am expecting answer to be 2, but when I am seeing body response it is empty array.

what might have gone wrong I am unable to get

rieckpil

As you're using JUnit Jupiter, you have to use @BeforeEach. @Before is from JUnit 4 and hence wasn't invoked as part of the test's lifecycle:

@BeforeEach
public void init() {
    List<Person> list = new ArrayList<>();
    Person p1 = new Person("dumm1", "lastName1",22);
    Person p2 = new Person("dumm2", "lastName2",32);
    p1.setId(1l);
    list.add(p2);
    list.add(p1);
    when(mockRepository.findAll()).thenReturn(list);
    
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mock bean unable to inject dependecy in unit test

From Dev

Inject mock class into method to unit test method

From Dev

Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs

From Dev

Mock IMemoryCache in unit test

From Dev

Doing bean inject in camel test

From Dev

inject dependency into filter for unit test

From Dev

inject dependency into filter for unit test

From Dev

Mock/Stub a RuntimeException in unit test

From Dev

Spock Mock not working for unit test

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Unit test a void method with Mock?

From Dev

Unable to mock Owin context in C# WEB API REST service unit test

From Dev

Unable to inject controller to Karma test

From Dev

Unable to inject dependency in Junit test

From Dev

How to mock the Symfony HttpClient as a dependecy?

From Dev

Unable to Inject CDI Bean with rest Easy

From Dev

How to inject a @Named bean into a Junit test

From Dev

How to inject a @Named bean into a Junit test

From Dev

Cannot inject service into its Angular unit test

From Dev

Jersey 2 inject dependencies into unit test

From Dev

Angular2 Inject ElementRef in unit test

From Dev

How to call methods that inject parameters in a unit test?

From Dev

Create object with name to inject into unit test

From Dev

Inject mock object without java interface for unit testing

From Dev

Test used bean instead mock object

From Dev

Unit test command class with mock repository

From Dev

How to android unit test and mock a static method

From Dev

Correct way to mock an AngularJS Service in a unit test?

Related Related

HotTag

Archive