@Override public Lifecycle newInstance( KernelContext context, Dependencies dependencies ) { dependencies.procedures().registerComponent( SomeService.class, ctx -> new SomeService(), true ); return new LifecycleAdapter(); }
SomeService myService = new SomeService(); public MyConstructor() { someService.doSomething(); }
@Test public void testDoThat() { try { new SomeService().doThat(); Assert.fail(); } catch(GenericException ex) { assertEquals(GenericException.class, ex.getClass()); } }
@Test public void testDoThat() { when(new SomeService()).doThat(); then(caughtException()).isExactlyInstanceOf(GenericException.class); }
providers: [{ provide: SomeService, useFactory: (http: Http) => { return new SomeService(http)}, deps: [Http] }]
@Component @RunWith(MockitoJUnitRunner.class) public class SomeTests { @Mock private FooBar foobar; @InjectMocks private final SomeService someService = new SomeService(); @Test @Transactional public void test(){ when(fooBar.doStuff()).then....; someService.blargh() ..... } }
URL newWsdlLocation = "http://theServerName:9999/somePath" SomeService someService = new SomeService(newWsdlLocation);
public static SomeService getInstance() { if(service == null) { service = new SomeService(); } **return service;** } public doGet(request, response) { ErrorStatus es = new ErrorStatus(); SomeService service = SomeService.getInstance(); es = service.doSomething(es); //<-- This first one never gets tracked; es is overwritten below. es = service.doSomethingElse(es); printErrors(response.getWriter(), es); } public ErrorStatus() { id = 0; **<-- How exactly does id increase?** status = ""; messages = new ArrayList<>(); }
@Bean public SomeService getSomeService() throws IOException { return new SomeService(System.getProperty("myproperty")); }
public class SomeServiceTest { @Rule public ExpectedException thrown= ExpectedException.none(); /** Just makes sure no exception is thrown */ @Test public void sortByFieldX() { List<MyClass> list = null; SomeService service = new SomeService(); service.sortByFieldX(list); } }
@Test(expected = NullPointerException.class) public void sortByFieldX() { List<MyClass> list = null; SomeService service = new SomeService(); service.sortByFieldX(list); }
//given SomeService service = new SomeService(); SomeService spy = Mockito.spy(service); //when spy.doSecond(); //then verify(spy).doFirst();
SomeService service = new SomeService(); service.setNumber(new Double(0));//or more vicious : service.setNumber(0.0)
return new SomeService();