import zio.Task; import zio.ZIO; public class UserService { public Task<User> getUserById(int id) { // ... return ZIO.succeed(new User(id, "John Doe")); } } public class User { private int id; private String name; // ... } import zio.Runtime; import zio.Task; import zio.test.Assertion; import zio.test.DefaultRunnableSpec; import zio.test.assert; import zio.test.environment.TestEnvironment; import zio.test.mock.Expectation; import zio.test.mock.Method; import zio.test.mock.MockSpec; public class UserServiceSpec extends DefaultRunnableSpec { private UserService userService = new UserService(); public static void main(String[] args) { Runtime.unsafeRun(new UserServiceSpec().spec.provideSome(TestEnvironment.any())); } public static MockSpec<?, ?> mockUserService() { return MockSpec.builder(UserService.class) .expect(new Method<>().getUserById(1)) .returns(Task.succeed(new User(1, "Mock User"))) .build(); } public spec() { suite("User Service")( testM("get user by ID") { assertM(userService.getUserById(1))(Assertion.equalTo(new User(1, "John Doe"))) } @@ mockUserService() ) } }


上一篇:
下一篇:
切换中文