junit - Spring Boot unit testing a Service, manual tests pass but mocked service fails -
in order test service logic have set initial test data in 3 repositories. bit of test working, updates service supposed make never happen in repositories , test fails. if show test followed service logic more clear i'm trying do.
i think i'm missing annotations mocking service properly. think mocked service not connected mocked repository. don't know specific configurations add.
here code:
test:
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 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 true 5 data items testrun.setresult(true); testrun.setgenerateddatacount((long)5); testrunservice.add(testrun); // test run result false 3 data items testrun.setresult(false); testrun.setgenerateddatacount((long)3); testrunservice.add(testrun); // test fails because testrunservice did not persist data system.out.println(testrunrepository.count()); //output = 0 // test tool , 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 nullpointerexception, generateddatacount not updated testrunservice } }
service:
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 test tool testsuite suite = testsuiterepository.findbyid(result.gettestsuite().getid()); testtool tool = testtoolrepository.findbyid(suite.gettesttool().getid()); tool.settotalruntime(result.getruntimemilliseconds()); // if run pass, update generated data count if (result.getresult() == true){ tool.setgenerateddatacount(result.getgenerateddatacount()); } // save updated test tool information testtoolrepository.save(tool); // update runtime total test case testcase testcase = testcaserepository.findbyid(suite.gettestcase().getid()); testcase.settotalruntime(result.getruntimemilliseconds()); // if run pass, update generated data count if (result.getresult() == true){ testcase.setgenerateddatacount(result.getgenerateddatacount()); } // save updated test case information testcaserepository.save(testcase); return responseentity.created(location).build(); } }
the thing know service works through manual testing , inspecting database, it's frustrating me can't unit test pass.
i'm not sure why using @mockbean annotation service.
can try constructing proper testrunservice object in setup method , use that?
for example:
@before public void createtestdata(){ //set service testrunservice = new testrunserviceimpl(); //invoke repository setters on service object //data creation }
Comments
Post a Comment