从Rest API生成Spring API文档

时间:2018-12-23 13:46:43

标签: java spring spring-rest junit5 spring-restdocs

我想为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) {
        .......
        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();   
    private MockMvc mockMvc;         
    @MockBean
    private PaymentTransactionRepository transactionRepository;        
    @BeforeEach
    void setUp() {                  
        Optional<PaymentTransactions> optional = Optional.of(new PaymentTransactions());       
        PaymentTransactionRepository processor = Mockito.mock(PaymentTransactionRepository.class);
        Mockito.when(processor.findById(Integer.parseInt("1"))).thenReturn(optional);       

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();      
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
              .apply(documentationConfiguration(restDocumentation)).build();
    }    
    @Test
    public void testNotNull() {
        assertNotNull(target);
    }    
    @Test
    public void testFindByIdFound() {
        Optional<PaymentTransactions> res = target.findById(Integer.parseInt("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();
        }
    }
}

但是我得到了NPE:

2018-12-23 15:31:10.411  INFO 48843 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : Completed initialization in 35 ms
java.lang.NullPointerException      org.datalis.admin.restapi.PaymentTransactionRepositoryIntegrationTest.uniqueTransactionIdLenght(PaymentTransactionRepositoryIntegrationTest.java:96)

在这一行:this.mockMvc.perform(RestDocumentationRequestBuilders.get("/transactions/1")).andExpect(status().isOk())

您知道我该如何解决这个问题?可能我的配置不正确?

完整错误堆栈:https://pastebin.com/U5GzjAQ4

0 个答案:

没有答案