1
Test ettiğim bir hizmet sınıfı tarafından kullanılmak üzere bir alay enjekte etmeye çalışıyorum ancak sahte görünmüyor.bahar enjekte alay kullanılmıyor
public class SpringDataJPARepo{ public void someMethod();// my repo }
i
@Service
public class Service implements IService{
@Autowired
private SpringDataJPARepo repository;
public String someMethod(){ // repository instance used here }
}
test etmek isteyen bir hizmet sınıfı var ve depo alay ve hizmet
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={ServiceTestConfiguration.class})
public class Test{
@Mock
private SpringDataJPARepo repository;
@Autowired
@InjectMocks
private IService service;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
when(repository.someMethod()).thenReturn("test");
}
@Test
public testSomeMethod(){
assertThat(service.someMethod()).isNotNull;
verify(repository.someMethod,atLeast(1)); //fails here
}
}
bunu içine enjekte ederek benim test durumları yazmaya çalışıyorum
i autowired örneğine alay enjekte nasıl üzerinde emin değilim. Burada yanlış yaptığım şeyi işaret edebilir mi? @Before yılındaİstenen ancak başlatılmadı 01 doğrulamak yöntemle
yılında
public class TestUtils {
/**
* Unwrap a bean if it is wrapped in an AOP proxy.
*
* @param bean
* the proxy or bean.
*
* @return the bean itself if not wrapped in a proxy or the bean wrapped in the proxy.
*/
public static Object unwrapProxy(Object bean) {
if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
Advised advised = (Advised) bean;
try {
bean = advised.getTargetSource().getTarget();
} catch (Exception e) {
e.printStackTrace();
}
}
return bean;
}
/**
* Sets a mock in a bean wrapped in a proxy or directly in the bean if there is no proxy.
*
* @param bean
* bean itself or a proxy
* @param mockName
* name of the mock variable
* @param mockValue
* reference to the mock
*/
public static void setMockToProxy(Object bean, String mockName, Object mockValue) {
ReflectionTestUtils.setField(unwrapProxy(bean), mockName, mockValue);
}
}
TestUtils.setMockToProxy(service, "repository", repository);
işleri İşlemsel "veya" @ Güvenli "? –
no başka herhangi bir ek açıklama yok – austin
Hizmet sınıfınızda "SpringDataJPARepo" dışında başka bir bağımlılık var mı? – Bunti