Posts

Showing posts with the label Testing

Cleanup After All Junit Tests

Answer : I'm using JUnit 4.9. Will this help?: import junit.framework.TestCase; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({First.class,Second.class,Third.class}) public class RunTestSuite extends TestCase { @BeforeClass public static void doYourOneTimeSetup() { ... } @AfterClass public static void doYourOneTimeTeardown() { ... } } Edit: I am quite positive (unless I misunderstand your question) that my solution is what you are looking for. i.e. one teardown method after all your tests have ran. No listener required, JUnit has this facility. Thanks. I recommend to use org.junit.runner.notification.RunListener, example: public class TestListener extends RunListener { @Override public void testRunStarted(Description description) throws Exception { // Called be...

Angular Click Select Option In Component Test

Answer : The way to change the selected option of a dropdown is to set the dropdown value and then dispatch a change event. You can use this answer as reference: Angular unit test select onChange spy on empty value In your case, you should do something like this: const select: HTMLSelectElement = fixture.debugElement.query(By.css('#dropdown')).nativeElement; select.value = select.options[3].value; // <-- select a new value select.dispatchEvent(new Event('change')); fixture.detectChanges(); You don't have to dispatch a change event. First you need to click on the trigger for your dropdown, i'm assuming it's your selectEl selectEl = fixture.debugElement.query(By.css('#dropdown')) . selectEl.click(); fixture.detectChanges(); After detectChanges your dropdown should be opened. Only after this will you be able to get your options from fixture, because before they where not present in your fixture. The only way I have been a...

Angular 7 Test: NullInjectorError: No Provider For ActivatedRoute

Answer : You have to import RouterTestingModule import { RouterTestingModule } from "@angular/router/testing"; imports: [ ... RouterTestingModule ... ] Add the following import imports: [ RouterModule.forRoot([]), ... ], I had this error for some time as well while testing, and none of these answers really helped me. What fixed it for me was importing both the RouterTestingModule and the HttpClientTestingModule. So essentially it would look like this in your whatever.component.spec.ts file. beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ProductComponent], imports: [RouterTestingModule, HttpClientTestingModule], }).compileComponents(); })); You can get the imports from import { RouterTestingModule } from "@angular/router/testing"; import { HttpClientTestingModule } from "@angular/common/http/testing"; I dont know if this is the best solution, but this worked for me....

Android Studio "No Tests Were Found"

Answer : Today I had the same problem with some Espresso tests and it was getting me crazy because everything seemed normal. Finally I discovered the problem was because the method annotated with @BeforeClass was throwing an exception. If something goes wrong in that method, the stacktrace of the exception is not shown in the Log window of the Run tab but in the Log window of the Android Monitor tab If you want to reproduce the problem just add this to your testing class: @BeforeClass public static void setupClass() { throw new RuntimeException("Sorry dude, you won't find any test!"); } This can happen when the type of your run configuration is incorrect. With me, this goes wrong when running an Espresso test which used to be a unit test. For some reason it still uses the Android JUnit test configuration when running this test. Manually creating an Android Instrumented Test solves the problem. Just for posterity sakes, here's the solution that worke...