Run JUnit tests with SBT

Finally, I’ve discovered, that I have to add the following settings to the subproject: lazy val webapp = project settings( Seq( projectDependencies ++= Seq( …. “org.scalatest” %% “scalatest” % “2.2.2” % Test, “junit” % “junit” % “4.11” % Test, crossPaths := false, “com.novocode” % “junit-interface” % “0.11” % Test ) ): _* ) It is … Read more

How to unit test a Go Gin handler function?

To test operations that involve the HTTP request, you have to actually initialize an *http.Request and set it to the Gin context. To specifically test c.BindQuery it’s enough to properly initialize the request’s URL and URL.RawQuery: func mockGin() (*gin.Context, *httptest.ResponseRecorder) { w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) // test request, must instantiate a request … Read more

Updating input html field from within an Angular 2 test

You’re right that you can’t just set the input, you also need to dispatch the ‘input’ event. Here is a function I wrote earlier this evening to input text: function sendInput(text: string) { inputElement.value = text; inputElement.dispatchEvent(new Event(‘input’)); fixture.detectChanges(); return fixture.whenStable(); } Here fixture is the ComponentFixture and inputElement is the relevant HTTPInputElement from the … Read more

What is the best way to unit-test SLF4J log messages?

Create a test rule: import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.slf4j.LoggerFactory; import java.util.List; import java.util.stream.Collectors; public class LoggerRule implements TestRule { private final ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); private final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); @Override public Statement apply(Statement base, Description description) { return new Statement() { @Override … Read more

Service mocked with Jest causes “The module factory of jest.mock() is not allowed to reference any out-of-scope variables” error

You need to store your mocked component in a variable with a name prefixed by “mock”. This solution is based on the Note at the end of the error message I was getting. Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable … Read more

tech