2016-11-17 92 views
0

爲了測試我的服務邏輯,我必須在3個存儲庫中設置一些初始測試數據。測試的這一點正在起作用,但是然後更新服務應該永遠不會發生在存儲庫中,並且測試失敗。希望如果我按照服務邏輯顯示測試,它會更清楚我想要做什麼。Spring Boot單元測試服務,手動測試通過但模擬服務失敗

我想我可能會錯過一些註釋來正確地嘲笑服務。我認爲嘲笑的服務沒有連接到其嘲諷的存儲庫。我不知道要添加哪些特定的配置。

下面是代碼:

測試:

package portal.services; 

import static org.junit.Assert.assertEquals; 

@RunWith(SpringJUnit4ClassRunner.class) 
@DataJpaTest 
public class TestRunServiceTest { 
    @MockBean 
    private TestRunService testRunService; 
    @Autowired 
    private TestRunRepository testRunRepository; 
    @Autowired 
    private TestCaseRepository testCaseRepository; 
    @Autowired 
    private TestToolRepository testToolRepository; 
    @Autowired 
    private TestSuiteRepository testSuiteRepository; 

    TestCase testCase = new TestCase(); 
    TestTool testTool = new TestTool(); 
    TestSuite testSuite = new TestSuite(); 
    TestRun testRun = new TestRun(); 

    @Before 
    public void createTestData(){ 
     // create the test case 
     testCase.setId(1); 
     testCase.setName("Agent Charities"); 
     testCaseRepository.save(testCase); 

     // create test tool 
     testTool.setId(1); 
     testToolRepository.save(testTool); 

     //create test suite 
     testSuite.setId(1); 
     testSuite.setTestCase(testCase); 
     testSuite.setTestTool(testTool); 
     testSuiteRepository.save(testSuite); 

     // create test run 
     testRun.setTestSuite(testSuite); 
     testRun.setStartDateTime(new Date()); 
     testRun.setEndDateTime(new Date()); 
    } 

    @Test 
    public void aggregateDataCountWhenResultIsPass(){ 

     // test run result is true with 5 data items 
     testRun.setResult(true); 
     testRun.setGeneratedDataCount((long)5); 
     testRunService.add(testRun); 

     // test run result is false with 3 data items 
     testRun.setResult(false); 
     testRun.setGeneratedDataCount((long)3); 
     testRunService.add(testRun); 

     // TEST FAILS BECAUSE testRunService did not persist any data 
     System.out.println(testRunRepository.count()); //output = 0 

     // test tool and test case repositories should show count of 5 
     assertEquals((long)5, testCaseRepository.findByid((long)1).getGeneratedDataCount().longValue()); 
     assertEquals((long)5, testToolRepository.findByid((long)1).getGeneratedDataCount().longValue()); 
     // test failed with NullPointerException, GeneratedDataCount was not updated by testRunService 
    } 

} 

服務:

public interface TestRunService { 
    Iterable<TestRun> listAllTestRuns(); 
    TestRun getTestRunById(Integer id); 
    ResponseEntity<?> add(@RequestBody TestRun input); 
} 


@Service 
public class TestRunServiceImpl implements TestRunService { 

    private TestRunRepository testRunRepository; 
    private TestSuiteRepository testSuiteRepository; 
    private TestCaseRepository testCaseRepository; 
    private TestToolRepository testToolRepository; 

    @Autowired 
    public void setTestRunRepository(TestRunRepository testRunRepository) { 
     this.testRunRepository = testRunRepository; 
    } 
    @Autowired 
    public void setTestSuiteRepository(TestSuiteRepository testSuiteRepository) { 
     this.testSuiteRepository = testSuiteRepository; 
    } 
    @Autowired 
    public void setTestCaseRepository(TestCaseRepository testCaseRepository) { 
     this.testCaseRepository = testCaseRepository; 
    } 
    @Autowired 
    public void setTesttOOLRepository(TestToolRepository testToolRepository) { 
     this.testToolRepository = testToolRepository; 
    } 

    @Override 
    public Iterable<TestRun> listAllTestRuns() { 
     return testRunRepository.findAll(); 
    } 

    @Override 
    public TestRun getTestRunById(Integer id) { 
     return testRunRepository.findOne((long)id); 
    } 

    @Override 
    public ResponseEntity<?> add(@RequestBody TestRun input) { 

     // save te test run 
     TestRun result = testRunRepository.save(input); 
     URI location = ServletUriComponentsBuilder 
       .fromCurrentRequest().path("/{id}") 
       .buildAndExpand(result.getId()).toUri(); 

     // update runtime total for test TOOL 
     TestSuite suite = testSuiteRepository.findByid(result.getTestSuite().getId()); 
     TestTool tool = testToolRepository.findByid(suite.getTestTool().getId()); 
     tool.setTotalRuntime(result.getRuntimeMilliseconds()); 

     // if the run was a pass, update the generated data count as well 
     if (result.getResult() == true){ 
      tool.setGeneratedDataCount(result.getGeneratedDataCount()); 
     } 
     // save updated test tool information 
     testToolRepository.save(tool); 

     // update runtime total for test CASE 
     TestCase testCase = testCaseRepository.findByid(suite.getTestCase().getId()); 
     testCase.setTotalRuntime(result.getRuntimeMilliseconds()); 

     // if the run was a pass, update the generated data count as well 
     if (result.getResult() == true){ 
      testCase.setGeneratedDataCount(result.getGeneratedDataCount()); 
     } 
     // save updated test case information 
     testCaseRepository.save(testCase); 

     return ResponseEntity.created(location).build();   
    } 
} 

是我知道服務的事情的作品通過手工測試和檢查數據庫,所以我不能通過單元測試讓我感到沮喪。

+0

爲什麼要它...你創建你的服務,讓您實現什麼是不會被執行的一個模擬...什麼你認爲'@ MockBean'是爲了? –

+0

我很好,我確定需要執行它,所以我該怎麼做?我嘗試了@Autowired,錯誤是'錯誤創建名爲'portal.services.TestRunServiceTest'的bean:通過字段'testRunService'表示的不滿意的依賴關係;嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合條件的bean for dependency [portal.services.TestRunService]:預計至少有1個bean有資格作爲autowire候選者。依賴註釋:' – clickcell

+1

我確實知道答案,但並不是告訴某人我想將它們指向包含答案的資源的答案。有點像我可以給男人一條魚,或者學習如何捕魚。 Bt如果你必須。將'@ DataJpaTest'替換爲'@SpringBootTest'(並將它指向你的應用程序類),用'@ Autowired'替換'@ MockBean',如果你的彈簧引導配置正確(你可能需要將它傳遞給'@SpringBootTest這些都會啓動一個完整的spring啓動測試,這也是我在你指出的文檔中所解釋的所有內容。 –

回答

0

我不確定爲什麼你要爲你的服務使用@MockBean註解。

你可以嘗試在你的setup方法中構造一個正確的TestRunService對象並使用它嗎?

例如:

@Before 
public void createTestData(){ 
    //set up service 
    testRunService = new TestRunServiceImpl(); 
    //invoke repository setters on your service object 
    //data creation  
} 
+0

謝謝,當我嘗試我得到的錯誤是'Can not instantiate TestRunService'類型 - 我刪除了@MockBean註釋 – clickcell

+0

testRunService = new TestRunServiceImpl(); –

+0

這將不起作用,因爲您將不會有事務代理 –