### Question:
Server { public int run() { PrivateKey serverKey; try { String server_key = options.findByLongOption("server-key").getValue(); if (server_key == null) { System.err.println("Missing Server key: --help --server-key for more information."); return 1; } else { serverKey = new PrivateKey(new File(server_key)); } } catch (IOException e) { System.err.println("**ERROR** Failed to read server-key from: " + options.findByLongOption("server-key")); return 1; } communications.setListenerKey(serverKey); return communications.listen(); } Server(CommandLine.Options options, UserStore userStore, Communications communications);  Server(CommandLine.Options options); int run();  }### Answer:
@Test public void testRun() throws Exception { Server server = new Server(opts, userStore, communications); CommandLine.Option option = createNiceMock(CommandLine.Option.class); expect(opts.findByLongOption("server-key")).andReturn(option); option.getValue(); expectLastCall().andReturn(ClassLoader.getSystemResource("test.pem").getFile()); communications.setListenerKey(anyObject(PrivateKey.class)); expectLastCall().once(); communications.listen(); expectLastCall().andReturn(0).once(); replay(opts, userStore, communications, option); server.run(); verify(communications); }
@Test public void testRunWithBadKey() throws Exception { expectedException.expect(OpenSSLRSAPEM.DER.MalformedDERException.class); Server server = new Server(opts, userStore, communications); CommandLine.Option option = createNiceMock(CommandLine.Option.class); expect(opts.findByLongOption("server-key")).andReturn(option); option.getValue(); expectLastCall().andReturn(ClassLoader.getSystemResource("test.der").getFile()); replay(opts, userStore, communications, option); server.run(); }
@Test public void testRunWithMissingKey() throws Exception { Server server = new Server(opts, userStore, communications); CommandLine.Option option = createNiceMock(CommandLine.Option.class); expect(opts.findByLongOption("server-key")).andReturn(option); option.getValue(); expectLastCall().andReturn("nothere.pem"); replay(opts, userStore, communications, option); assertEquals(1, server.run()); }