I've a TestClass
with two Tests foo()
and bar()
, foo
uses the original bean serviceObject
, while bar
uses a mockedBean: so always one test fails. is there any way to mock my bean only for bar()
@RunWith(SpringRunner.class)
@SpringBootTest
pubic class TestClass{
@MockBean
private ServiceObject serviceObject;
@Test
public void foo(){
//do smt test with non mocked bean serviceObject
}
@Test
public void bar(){
//do smt with mockedBean
}
}
CodePudding user response:
I would split up the tests into two different classes, as they obviously test something completely different.
One (foo()
) probably tests the behaviour of the ServiceBean
implementation, the other (bar()
) tests something that uses a ServiceBean
instance.
CodePudding user response:
Just use the object that you need to use in any given test. Here is some example code:
@RunWith(SpringRunner.class)
@SpringBootTest
pubic class TestClass{
@MockBean
private ServiceObject mockServiceObject;
@Test
public void foo(){
ServiceObject blam = new ServiceObject();
// Use blam in this test
//do smt test with non mocked bean serviceObject
}
@Test
public void bar(){
//do smt with mockedBean
// use mockServiceObjec in this test
}
}
CodePudding user response:
I assume you can create one instance with @Autowired
annotation in class scope and mock it in method scope.