2011-01-07 15 views

cevap

12

: mockito javadoc veya ben hakkında yazdığı bir blog post daha yaklaşık @InjectMocks oku

@RunWith(MockitoJUnitRunner.class) 
public class RuleIdValidatorTest { 
    @Mock 
    private RuleStore ruleStoreMock; 

    @InjectMocks 
    private RuleIdValidator ruleIdValidator; 

    @Test 
    public void someTest() { 
     when(ruleStoreMock.doSomething("arg")).thenReturn("result"); 

     String actual = ruleIdValidator.doSomeThatDelegatesToRuleStore(); 

     assertEquals("result", actual); 
    } 
} 

Bu Stackoverflow sonrası Bunu yapmanın iyi bir örnek vardır konu bir süre önce.

1.9.0 sürümünde geliştirilmiş olan Mockito 1.8.3 sürümünden itibaren kullanılabilir.

2

Aşağıdaki yapabilirsiniz:

package com.mycompany;  

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.DependsOn; 
import org.springframework.stereotype.Component; 

@Component 
@DependsOn("ruleStore") 
public class RuleIdValidator implements ConstraintValidator<ValidRuleId, String> { 

    @Autowired 
    private RuleStore ruleStore; 

    // Some other methods 
} 

Ve Bahar Bağlam gerektiği gibi görünür:

<context:component-scan base-package="com.mycompany" /> 

<bean id="ruleStore" class="org.easymock.EasyMock" factory-method="createMock"> 
    <constructor-arg index="0" value="com.mycompany.RuleStore"/> 
</bean> 
İlgili konular