Skip to content

Commit

Permalink
#182 README: demo MockVaadin.setMockRequestFactory() in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Jan 16, 2025
1 parent 95f93fe commit ace232c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions karibu-testing-v10-spring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ MockVaadin.mockRequestFactory = {
}
}
```
Java:
```java
MockVaadin.INSTANCE.setMockRequestFactory(session -> new FakeRequest(session) {
@Override
public @Nullable Principal getUserPrincipal() {
return null; // SecurityContextHolder.getContext().getAuthentication()
}
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public object MockVaadin {
* Creates [MockRequest]; override if you need to return a class that extends [MockRequest]
* and modifies its behavior.
*/
@JvmStatic
public var mockRequestFactory: (FakeHttpSession) -> FakeRequest = { FakeRequest(it) }

private fun createSession(ctx: ServletContext, uiFactory: () -> UI) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.github.mvysny.kaributesting.v10;

import com.github.mvysny.fakeservlet.FakeRequest;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinResponse;

import jakarta.servlet.http.Cookie;
import org.jetbrains.annotations.Nullable;

import java.security.Principal;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -22,5 +26,11 @@ public MockVaadinJApiTest() {
UtilsKt.getFake(VaadinRequest.getCurrent()).addCookie(new Cookie("foo", "bar"));
return new LocatorJApiTest.MyUI();
});
MockVaadin.INSTANCE.setMockRequestFactory(session -> new FakeRequest(session) {
@Override
public @Nullable Principal getUserPrincipal() {
return null; // SecurityContextHolder.getContext().getAuthentication()
}
});
}
}

0 comments on commit ace232c

Please sign in to comment.