0

Currently, I am working on the sample demo application for learning purposes, in which I have implemented the CRUD operations for the Tutorial.

Here is my sample code to create a new tutorial:

TutorialsController.java

public class TutorialsController {

    @Autowired
    TutorialService tutorialService;

    @PostMapping("/tutorials")
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {
    try {
        Tutorial _tutorial = tutorialService.createTutorial(tutorial);
        return new ResponseEntity<>(_tutorial, HttpStatus.CREATED);
    } catch (Exception | BaseException e) {
        logger.info("Error:: {}", e.getMessage());
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
}

TutorialServiceImpl.java

@Service
public class TutorialServiceImpl implements TutorialService {
 public Tutorial createTutorial(Tutorial tutorial) {
    If(Utility.checkToggle) {
        throw new BadRequestException("Validation faild for given tutorial. Please check provided parameters")
    } else {
        return tutorialRepository.save(
                new Tutorial(tutorial.getTitle(), tutorial.getDescription(), tutorial.getAuthor(), false)
        );
    }
}
}

I have a static method checkToggle in the Utility class which performs some toggle check and if the given check gets failed I am raising BadRequestException.

Now I am trying to test the scenario where the toggle check is failed. Here is my sample code

@RunWith(PowerMockRunner.class)
@PrepareForTest({TutorialsController.class})
public class TutorialsControllerTest {
    @InjectMocks
    TutorialsController tutorialsController;

    @Mock
    TutorialServiceImpl tutorialServiceImpl;

    @Test
    public void test1() {
        Tutorial tutorial = new Tutorial();
        tutorial.setAuthor("Test");
        tutorial.setDescription("Test");
        PowerMockito.mockStatic(Utility.class);
        when(Utility.checkToggle()).thenReturn(true);
        ResponseEntity<Tutorial> response = tutorialsController.createTutorial(tutorial);
        assertEquals(response.getStatusCode(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}

But my test case is failing. After debugging found that Utility.checkToggle() method is not getting mocked.

So can you please let me know why this is happening? And how to fix this.

For implementing the test case I followed https://ngdeveloper.com/how-to-test-controller-using-powermock-in-spring-boot/ tutorial.

Gol. D Roger
  • 119
  • 1
  • 7
  • If `Utility` is under your control, you should change its methods from static to instance methods. This is easier to test and also better if you plan to do dependency injection. – cyberbrain Apr 22 '22 at 06:37

1 Answers1

0

You need to specify Utility.class in the list of the @PrepareForTest annotations parameters.

On the other hand, the class under test (TutorialsController.class) should not be listed there, as you probably don't want PowerMock to manipulate its bytecode - otherwise you will not test the "real thing".

Btw: Don't know if you use that, but since Mockito 3.4.0 also mocking of static methods is possible, see this answer on SO

cyberbrain
  • 3,433
  • 1
  • 12
  • 22