4

Can someone provide any sample on this scenario combination. I gave a sample snippet on the lines below, which is failing while trying to mock, intent is to combine both MockMVC with PowerMock

Create a spring Boot Test App for integration testing with

1) MockMvc for initiating a call from controller till end 2) PowerMock to Mock static methods in dependent class

@RunWith(SpringRunner.class)
@PowerMockRunnerDelegate(PowerMockRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@PrepareForTest(ClasswithStaticMethod.class)
public class DevLocationTest {


    @Autowired
    private MockMvc mockMvc;

    @MockBean
    ABCRepository abcRepository;

    @MockBean
    private ClasswithStaticMethod instance;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);        
    }


    @Test
    public void test() throws Exception {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization","");

        PowerMockito
                .when(abcRepository
                        .findByDevIdAndPropertyEventNameIn(devId,
                                Arrays.asList(eventName)))
                .thenReturn(devPropMapping);


        PowerMockito.mockStatic(ClasswithStaticMethod.class);
        Mockito.when(ClasswithStaticMethod.getInstance())
         .thenReturn(instance);

which is failing while trying to mock "ClasswithStaticMethod" with error like below, even though i have given @PrepareForTest(ClasswithStaticMethod.class)

The class com.XXX.services.config.ClasswithStaticMethod not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation.

0 Answers0