我想为Rest api创建JUnit测试并生成api doc。我想测试这段代码:
其他控制器
@RestController
@RequestMapping("/transactions")
public class PaymentTransactionsController {
@Autowired
private PaymentTransactionRepository transactionRepository;
@GetMapping("{id}")
public ResponseEntity<?> get(@PathVariable String id) {
return transactionRepository
.findById(Integer.parseInt(id))
.map(mapper::toDTO)
.map(ResponseEntity::ok)
.orElseGet(() -> notFound().build());
}
}
存储库界面
public interface PaymentTransactionRepository extends CrudRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {
Optional<PaymentTransactions> findById(Integer id);
}
存储库Impl:
@Service
@Qualifier("paymentTransactionService")
@Transactional
public class PaymentTransactionRepositoryImpl implements PaymentTransactionRepository {
@Override
public Optional<PaymentTransactions> findById(Integer id) {
String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.id = :id";
TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class).setParameter("id", id);
List<PaymentTransactions> paymentTransactions = query.getResultList();
return paymentTransactions.isEmpty() ? Optional.empty() : Optional.of(paymentTransactions.get(0));
}
}
我尝试使用Mockito实现此JUnit5测试:
@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class })
@SpringBootTest(classes = PaymentTransactionsController.class)
@WebAppConfiguration
public class PaymentTransactionRepositoryIntegrationTest {
public final ManualRestDocumentation restDocumentation = new ManualRestDocumentation();
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
PaymentTransactionRepository target;
@Autowired
private ObjectMapper objectMapper;
private MockMvc mockMvc;
@MockBean
private PaymentTransactionRepository transactionRepository;
@BeforeEach
void setUp() {
Optional<PaymentTransactions> optional = Optional.of(new PaymentTransactions());
when(transactionRepository.findById(Integer.parseInt("1"))).thenReturn(optional);
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
PaymentTransactionRepository tnx = Mockito.mock(PaymentTransactionRepository.class);
ReflectionTestUtils.setField(PaymentTransactionsController.class, "PaymentTransactionRepository", tnx);
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation)).build();
}
@Test
public void testNotNull() {
assertNotNull(target);
}
@Test
public void testFindByIdFound() {
Optional<PaymentTransactions> res = target.findById((int) 1);
assertTrue(res.isPresent());
}
@Test
public void uniqueTransactionIdLenght() {
try {
this.mockMvc.perform(RestDocumentationRequestBuilders.get("/transactions/1")).andExpect(status().isOk())
.andExpect(content().contentType("application/xml;charset=UTF-8"))
.andDo(document("persons/get-by-id"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我收到when
的错误消息:
The method when(Optional<PaymentTransactions>) is undefined for the type PaymentTransactionRepositoryIntegrationTest
您知道实施模拟测试的正确方法是什么吗?