gpt4 book ai didi

java - 模拟未被注入(inject)

转载 作者:行者123 更新时间:2023-12-01 19:54:23 24 4
gpt4 key购买 nike

我正在尝试为单元测试注入(inject)模拟,但我不断收到空点异常。

我要测试的类在下面,空指针似乎与 ParameterFacade 一起。

当我运行测试时,它似乎没有注入(inject)模拟,因为 ParameterFacade 为空。

@Service
public class PostUnderwriterTransactionProcessProvider extends AbstractProcessProvider<PostUnderwriterTransactionInput> {

@Autowired
private ParameterFacade parameterService;

@Override
public Page startProcess(PostUnderwriterTransactionInput processInput) {

final PostUnderwriterTransactionContext context = new PostUnderwriterTransactionContext(processInput.getPolicy(),
setupStrataContextModel(POST_TRANSACTIONS),
processInput.getTransactionsTypes(),
processInput.getParameterSelectionCriteria(),
parameterService.shouldApplyCommissionFromPolicy());

context.setPostingFromInput(processInput.getAccount().getBalance(),
processInput.getAccount().getID(),
getBranch(),
processInput.getPolicy(),
processInput.getProcess(),

null);

return new AdhocTransactionPostingPage(new TransactionPostingContextModel(context));
}

}

测试是

@RunWith(MockitoJUnitRunner.class)
public class PostUnderwriterTransactionProcessProviderTest extends WicketTestCase {

@ClassRule
public static MetadataServiceRule metadataServiceRule = new MetadataServiceRule();

@Mock
private ParameterFacade mockParameterService;

@InjectMocks
private PostUnderwriterTransactionProcessProvider testSubject;


@Before
public void setup() {
tester.putBean(mockConversationScopeManager);
testSubject = new PostUnderwriterTransactionProcessProvider();
policy = PolicyTestDataBuilder.aPolicy().build();
account = createAccount();
testSubject.setUserInfoProvider(MockUserInfoPartyProvider.getMockUserInfoProvider());
testSubject.setSystemPartyFacade(MockUserInfoPartyProvider.getMockSystemPartyFacade());
testSubject.setCodeModelFacade(codeModelFacade);
}

@Test
public void startProcessShouldCreateAdhocTransactionPostingPage() {
// Given
when(mockParameterService.shouldApplyCommissionFromPolicy()).thenReturn(true);
final ParameterSelectionCriteriaModel pscm = new ParameterSelectionCriteriaModel();
final List<TransactionTypeModel> transactions = createTransactionsTypes();
final PostUnderwriterTransactionInput input = new PostUnderwriterTransactionInput(policy, account, pscm, transactions);

// When
final Page page = testSubject.startProcess(input);

// Then
assertThat(page, instanceOf(AdhocTransactionPostingPage.class));
assertThat("Page Default Model is: ", page.getDefaultModelObject(), instanceOf(PostUnderwriterTransactionContext.class));
assertPageContextValues(page);
}
}

最佳答案

    1. ParameterFacade is a spring container managed bean (as it is annotated with @Service and other spring annotations like @Autowired).
2. @RunWith is configured to use MockitoJUnitRunner which cannot instantiate spring managed container beans

Hence, in order to handle bean lifecycle in unit tests you'll need to
1. Use @RunWith(SpringJUnit4ClassRunner.class)
2. Annotate PostUnderwriterTransactionProcessProvider with @Autowired.
3. Change @Mock to @MockBean on ParameterFacade (org.springframework.boot.test.mock.mockito.MockBean)

关于java - 模拟未被注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59052665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com