mardi 27 janvier 2015

Want spring bean to have mocked as well as real implementations+spring+mockito

I have a class Service which I want to test.Basically I am facing an issue as I want Service object to be parially mocked .Only object1 should be mocked rest of other colloborators should be real ones.My code is similar to below one:



@Component
public class Service {

@Autowired
Object1 obj1;

@Autowired
Object2 obj2;

public void validate(){
obj1.isValid();
obj2.isActive();

}


Now while testing validate method of the above Service class,I want to mock the obj1.isValid() method while invoking real implementation of the obj2



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:conf/test-context.xml"})
public class ServiceTest {

@InjectMocks
Service service;

@Mock
OBject1 obj1;

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

}


@Test
public void testValidate (){
service.validate();
//assert and other stuff
}
}


this test fails and when I debugged validate method.I found that object1 instance is injected as mocked as expected in the Serviceclass but Object2 is injected as null.Hence the test fails... Is there any way to achieve this ?


Aucun commentaire:

Enregistrer un commentaire