JUnit 5 extension for easy injection of Red and Blue objects
neboskreb / red-and-blue Goto Github PK
View Code? Open in Web Editor NEWJUnit 5 extension for easy injection of Red and Blue objects
License: Apache License 2.0
JUnit 5 extension for easy injection of Red and Blue objects
License: Apache License 2.0
Curerntly, the same instance is injected at all injection points. I.e., this fails:
@Test
private foo(@BlueInstance one, @BlueInstance another) {
assertNotSame(one, another);
}
This can be dangerous if the test modifies the content of the injected instance, as this change will be visible through all other injected blue instances, thus violating the expectations in the other (concurrently executed) tests or later in the same test.
Before injection, create a deep copy of the "etalon instance", and inject that copy instead.
Once the objective achieved, this should pass (pseudocode):
@Test
private foo(@BlueInstance one, @BlueInstance another) {
assertDeepNotSame(one, another);
assertDeepReflectionEqueals(one, another);
}
Deep copy is best for the test isolation but comes at performance price. The latter should be profiled, and if found insignificant then the default should be set to deep copy.
A way of configuration must be provided to control this behavior. For example, class-level annotation @RedAndBlueSettings
with field createCopies
would work nicely.
When object factory fails to construct a prefab automatically (e.g. because the type is abstract or non-instantiable), an exception is thrown which explains very little about the problem and difficult to enterpret.
Receiver class nl.jqno.equalsverifier.internal.reflection.PublicKey$$DynamicSubclass$41294f8$ does not define or inherit an implementation of the resolved method 'abstract byte[] getEncoded()' of interface java.security.Key.
java.lang.AbstractMethodError: Receiver class nl.jqno.equalsverifier.internal.reflection.PublicKey$$DynamicSubclass$41294f8$ does not define or inherit an implementation of the resolved method 'abstract byte[] getEncoded()' of interface java.security.Key.
at core.security.PublicKeyEncoding.encodeX509(PublicKeyEncoding.java:47)
at core.state.EncryptionKeysMapper.encodePublicKey(EncryptionKeysMapper.java:34)
at core.state.EncryptionKeysMapperImpl.encryptionKeysToPublicKeyEntity(EncryptionKeysMapperImpl.java:52)
at core.state.EncryptionKeysMapperImpl.toEntity(EncryptionKeysMapperImpl.java:25)
at core.state.EndpointAppKeysMapperImpl.toEntity(EndpointAppKeysMapperImpl.java:29)
at state.EndpointAppKeysMapperTest.testTransitive(EndpointAppKeysMapperTest.java:33)
Such exception should be wrapped into a higher-level exception explaining the problem and possible solution (i.e., Consider introducing a prefab for PublicKey
)
public class EndpointAppKeys {
private CipherSuite cipherSuite;
private EncryptionKeys clientKeys;
}
public class EncryptionKeys {
private PublicKey publicKey; // <-- PublicKey is interface. It declares but never defines `public byte[] getEncoded()`, hence the failure to instantiate the mock
}
@ExtendWith(RedAndBlueExtension.class)
public class EndpointAppKeysMapperTest {
@Test
void testTransitive(@RedInstance EndpointAppKeys red, @BlueInstance EndpointAppKeys blue) {
...
}
}
Support Java 11
Currently supported min version is 17
Abnormal behavior was reported by @nextincrement
The issue I encountered is that. when defining an instance of JsonNode as a prefab value, some error is raised because the abstract class JsonNode does not meet some requirements - while it's concrete implementation (so, not the declared class but the actuall class) should be fine. I know this because, when using equals-verifier, I can succesfully define two prefab values for an instance of JsonNode.
...
However, this [instantiating builders] does unfortunately not apply to the JsonNode as this type is also used in any builder (JsonNode in -> JsonNode out). I think that a very simple minimal test case: creating a red/blue instance for a class using just a JsonNode field (or maby two fields, including a simple string) may reveil the bug. If my recollection is right, it should fail immediately but can't test it right now due to some compiler errors I need to fix first.
Needs investigation
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/gradle-publish.yml
actions/checkout v4
actions/setup-java v4
gradle/wrapper-validation-action f9c9c575b8b21b6485636a91ffecd10e558c62f6
gradle/gradle-build-action ac2d340dc04d9e1113182899e983b5400c17cda1
.github/workflows/gradle.yml
actions/checkout v4
actions/setup-java v4
gradle/actions v4.0.0@af1da67850ed9a4cedd57bfd976089dd991e2582
mikepenz/action-junit-report v4
actions/checkout v4
actions/setup-java v4
gradle/actions v4.0.0@af1da67850ed9a4cedd57bfd976089dd991e2582
gradle.properties
settings.gradle
build.gradle
red-and-blue/build.gradle
nl.jqno.equalsverifier:equalsverifier 3.16.1
org.junit.jupiter:junit-jupiter-api 5.10.3
org.junit.jupiter:junit-jupiter-params 5.10.3
org.junit.jupiter:junit-jupiter-engine 5.10.3
org.junit.jupiter:junit-jupiter-api 5.10.3
org.junit.jupiter:junit-jupiter-params 5.10.3
org.junit.jupiter:junit-jupiter-engine 5.10.3
org.mockito:mockito-core 5.12.0
org.mockito:mockito-junit-jupiter 5.12.0
gradle/wrapper/gradle-wrapper.properties
gradle 8.9
The following example fails with exception "java.lang.IllegalStateException: Missing RED prefab of type null":
public class Entity {
private final EntityColor color;
}
public class EntityBuilder {
private Supplier<EntityColor> colorSupplier;
public Entity build() {
return new Entity(colorSupplier.get());
}
}
@ExtendWith(RedAndBlueExtension.class)
public class EndpointConnectionMapperTest {
@PrefabRed
private Supplier<EntityColor> prefabRed = () -> new EntityColor("red");
@PrefabBlue
private Supplier<EntityColor> prefabBlue = () -> new EntityColor("blue");
@Test
void testTransitive(@RedInstance EntityBuilder redBuilder, @BlueInstance EntityBuilder blueBuilder) {
// GIVEN
Entity red = redBuilder.build();
Entity blue = blueBuilder.build();
...
}
}
The exception trace attached as file exception_trace.txt
Currently, at Maven Central it shows up as production dependency:
// https://mvnrepository.com/artifact/io.github.neboskreb/red-and-blue
implementation 'io.github.neboskreb:red-and-blue:1.1.0'
Update the Maven metadata so the scope is shown as test
:
// https://mvnrepository.com/artifact/io.github.neboskreb/red-and-blue
testImplementation 'io.github.neboskreb:red-and-blue:1.1.0'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.