I am trying to mock abstract class which is called within another class. I mocked the abstract class however mocked abstract class is not being injected.
Any advise on how to mock the abstract class and inject it?
public abstract class MyAbstractClass {
public HelloBean getHelloBean(HelloBean bean){
return bean;
}
}
public class MyBusinessClass extends MyAbstractClass {
public String getBusinessData(){
HelloBean bean = getHelloBean(new HelloBean()) //I want to mock this method while testing getBusinessData()
}
}
My JUnit Class
public class MyBusinessClass {
private MyAbstractClass myAbstractClass = mock(MyAbstractClass.class);
private MyBusinessClass myBusinessClass = mock(MyBusinessClass.class);
#Test
public String getBusinessData(){
when(myAbstractClass.getHelloBean(any(HelloBean.class))).doReturn(new HelloBean());
myBusinessClass.getBusinessData();
}
}
By using Inject Mock it works
Here the test class is
#SpringBootTest
#AutoConfigureMockMvc
public class HelloServiceMockTest {
#InjectMocks
MyBusinessClass myBusinessClass;
#Mock
MyAbstractClass myAbstractClass;
#Mock
HelloBean helloBean;
#Test
public void getBusinessData(){
when(myAbstractClass.getHelloBean(helloBean)).thenReturn(new HelloBean());
myBusinessClass.getBusinessData();
Assert.assertEquals("ggg",myBusinessClass.getBusinessData());
}
}
HelloBean class is
public class HelloBean {
public String get()
{
return "ggg"; }
}
MyBusinessClass is
public class MyBusinessClass extends MyAbstractClass {
public String getBusinessData(){
HelloBean bean = getHelloBean(new HelloBean()); //I want to mock this method while testing getBusinessData()
return bean.get();
}
}
Related
public class UserController {
#Autowired
private UserRepository userRepository;
#GetMapping
public List<User> findAllUsers() {
return (List<User>) userRepository.findAll();
}
}
This is the code of the controller class
you specified the url in the #RequestMapping("...") annotation
#RequestMapping("url")
public class UserController {
#Autowired
private UserRepository userRepository;
#GetMapping
public List<User> findAllUsers() {
return (List<User>) userRepository.findAll();
}
and add the #RestController annotation above the class
Hello Guys I am trying to test my controller but i am getting nullpointException at Mockito "when" method in the class TodoControllerTest.getAllToDos() ( please see the pic ), I dont know why?
#ExtendWith(value ={SpringExtension.class})
#WebMvcTest
public class TodoControllerTest {
#Autowired
MockMvc mockMvc;
#MockBean
private ToDoService toDoService;
#Test
public void getAllToDos() throws Exception {
List<ToDo> toDoList = new ArrayList<ToDo>();
toDoList.add(new ToDo(1L,"Eat Eggs",true));
toDoList.add(new ToDo(2L,"Sleep Twice",true));
when(toDoService.findAll()).thenReturn(toDoList);
mockMvc.perform(MockMvcRequestBuilders.get("/todos")
.contentType(MediaType.APPLICATION_JSON)
).andExpect(jsonPath("$", hasSize(2))).andDo(print());
}
}
#Service
public class ToDoService {
public List<ToDo> findAll() {
return new ArrayList<ToDo>();
}
}
It works fine when I use #RunWith(SpringRunner.class) instead #ExtendWith(value ={SpringExtension.class})
because the prject with JUnit4
Let's take this simple JUnit test which runs with a CdiRunner.
#RunWith(CdiRunner.class)
public class MyTests {
#Inject
MyService service;
#Test
public void testFoo() {
service.doSomething();
Assert.assertTrue(true);
}
}
I'd like to extend this test so that it uses the Parameterized.classof JUnit. However additionally annotating the class does not work.
#RunWith(CdiRunner.class)
#RunWith(Parameterized.class)
public class MyTests {
#Inject
MyService service;
#Test
public void testFoo() {
service.doSomething();
Assert.assertTrue(true);
}
}
How can I implment a data-driven test using the CdiRunner?
I have camel route as below
public class IncomingBatchFileRoute extends RouteBuilder {
#Value(CCS_PROCESSING_INCOMING_DIRECTORY)
private String source;
#Override
public void configure() throws Exception {
from(sourceLocation)).autoStartup(false).to("encryptionEndPoint");
}
}
I need to write a JUNIT For above camel route and am new to it and created a structure as below
public class IncomingBatchFileRouteTest extends CamelTestSupport{
#Override
public RoutesBuilder createRouteBuilder() throws Exception {
return new IncomingBatchFileRoute();
}
#Test
public void sampleMockTest() {
}
}
Not sure how to complete it. Request you to help me on this
You need to mock your encryptionEndPoint and start your route with a producerTemplate
#Produce(uri = CCS_PROCESSING_INCOMING_DIRECTORY)
protected ProducerTemplate template;
#EndpointInject(uri = "encryptionEndPoint")
protected MockEndpoint resultEndpoint;
#Test
public void sampleMockTest() {
// GIVEN
this.resultEndpoint.expectedMessageCount(1);
// WHEN
this.template.sendBody("Hey");
// THEN
this.resultEndpoint.assertIsSatisfied();
}
I would like to be able for Mockito to perform a custom action when a given void method is called.
Say I have the following code:
#Autowired
private ProfileService profileService;
#Autowired
private ProfileDao profileDao;
private List<Profile> profiles;
#Before
public void setup() {
Mockito.when(profileDao.findAll()).thenReturn(profiles);
Mockito.when(profileDao.persist(any(Profile.class))).thenAddProfileToAboveList...
}
#Configuration
public static class testConfiguration {
#Bean
public ProfileDao ProfileDao() {
return mock(ProfileDao.class);
}
}
Say I want to add a Profile instance to the profiles list. Can Mockito do that? If so how?
Use Mockito.doAnswer.
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
// make the changes you need here
}})
.when(mock).someMethod();