2021-01-09

@RunWith(SpringRunner.class) vs @RunWith(SpringJUnit4ClassRunner.class)

 




What is the difference between SpringJUnit4ClassRunner and SpringRunner



Source URL: https://stackoverflow.com/a/50522420

.

@RunWith(SpringRunner.class) tells JUnit to run using Spring’s testing support. SpringRunner is the new name for SpringJUnit4ClassRunner, it’s just a bit easier on the eye.

SpringRunner is only available on spring-test 4.3.

SpringRunner class extends SpringJUnit4ClassRunner.

Source code of SpringRunner is

package org.springframework.test.context.junit4;

import org.junit.runners.model.InitializationError;

public final class SpringRunner extends SpringJUnit4ClassRunner {

    public SpringRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

}
.








Maven Parent vs Dependency

The two approaches are different, one is inheritance, and the other one is a simple dependency.

By dependency you'll only have the binary transitive dependencies of the project B.

By using as a parent project you'll inherit the configurations like plugins, building model, repositories, dependency-management, dependencies and so on, it depends on the case.

My rule of thumb is for scm configuration, project configuration, and development or company standards, I use a parent project (inheritance).

.

@SpringBootTest vs @DataJpaTest vs @WebMvcTest

 
In Short

  • @SpringBootTest loads full application context, exactly like how you start a Spring container when you run your Spring Boot application.

  • @WebMvcTest loads only the web layer, which includes security, filter, interceptors, etc for handling request/response. Typically you would write tests for methods under @Controller or @RestController.

  • @DataJpaTest loads only configuration for JPA. It uses an embedded in-memory h2 if not specified otherwise.

  • Service layer tests should ideally not have any annotations (except for ones that aid in mocking) because this is where your business logic (independent of any configurations) sits.

Regarding best practice, it's really just separation of concerns. I rarely ever used @SpringBootTest unless it's meant for some ad-hoc integration test on my local. Annotations like @WebMvcTest keep your tests more 'modularized' and slightly faster.
















2021-01-08

JUnit5 @RunWith

 

In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation.




.

4. Migrating from a JUnit4-Based Runner 

Let's now migrate a test that uses a JUnit4-based runner to JUnit5.

We're going to use a Spring test as an example:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

If we want to migrate this test to JUnit5 we need to replace the @RunWith annotation with the new @ExtendWith:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

The SpringExtension class is provided by Spring 5 and integrates the Spring TestContext Framework into JUnit 5. The @ExtendWith annotation accepts any class that implements the Extension interface.

.

In Short: What's JUnit 5

 
Source URL: https://stackoverflow.com/a/58384499

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage




Source URL: https://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5

.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. Furthermore, the platform provides a Console Launcher to launch the platform from the command line and a JUnit 4 based Runner for running any TestEngine on the platform in a JUnit 4 based environment. First-class support for the JUnit Platform also exists in popular IDEs (see IntelliJ IDEAEclipseNetBeans, and Visual Studio Code) and build tools (see GradleMaven, and Ant).

JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.

JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

Video Briefing



What's JUnit Platform?
You only need the launcher when you want to start a JUnit platform run programmatically, i.e. outside an IDE, build tool or console runner.

In other words: the launcher is the API being used by IDEs and build tools






2021-01-07

Best Practice: com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper()


private static final ObjectMapper jsonMapper = new ObjectMapper();

Constructing an ObjectMapper instance is a relatively expensive operation, so it's recommended to create one object and reuse it. You did it right making it final.

// Suggestion 1:
public static <T> T toObject1(final Class<T> type, final String json) throws IOException {
    return jsonMapper.readValue(json, type);
}

You always read JSON to a POJO, so let's be precise and clear, and use ObjectReader.

// Suggestion 2:
public static <T> T toObject2(final Class<T> type, final String json) throws IOException {
    return jsonMapper.readerFor(type).readValue(json);
}

// Suggestion 3:
public static <T> T toObject3(final Class<T> type, final String json) throws IOException {
    return jsonReader.forType(type).readValue(json);
}

There is no difference, really. Both methods will construct a new ObjectReader object: the former (jsonMapper.readerFor(type)) will give you a fully-built instance directly, the latter (jsonReader.forType(type)) will complement the not-yet-usable jsonReader and returns a ready-to-use object. I would rather go with option 2 because I don't want to keep that field.

You shouldn't worry about performance or thread-safety. Even though creating an ObjectMapper might be costly (or making a copy out of it), getting and working with ObjectReaders is lightweight and completely thread-safe.

From the Java documentation (emphasis mine):

Uses "mutant factory" pattern so that instances are immutable (and thus fully thread-safe with no external synchronization); new instances are constructed for different configurations. Instances are initially constructed by ObjectMapper and can be reused, shared, cached; both because of thread-safety and because instances are relatively light-weight.

I recently had these questions myself and decided on ObjectMapper#reader(InjectableValues) as a factory method. It's very handy particularly when you want to customise an ObjectReader slightly or, as it was in my case, to adjust a DeserializationContext.

That's an excellent question, by the way.

  improve this answer   





2021-01-03

Show only target phone on DXOMark

 










https://www.dxomark.com/rankings/#smartphones



Script #1

$.each( $( "div.col.deviceName.small-4" ), function( index, item ){ if ( index > 2 ){}else{ console.log( item ); } });




Script #2

var ls = $( "div.col.deviceName.small-4" ); console.log(`size of $(ls.length)`); console.log( ls.length ); $.each( ls, function( index, item ){ if( true )console.log( $( item ).text() + "|" + $( item ).text().toUpperCase().includes("XIAOMI") + "." );}); "";




Script #3

Last working executed on: 20210412_pm015753

var target = "IPHONE"; var ls = $( "div.col.deviceName.small-4" ); console.log(`size of $(ls.length)`); console.log( ls.length ); $.each( ls, function( index, item ){ var isTarget = $( item ).text().toUpperCase().includes( target ); var parentRow = $( item ).parent().parent().parent(); if( isTarget ) {console.log( $( item ).text() + "|" + isTarget + "|" + $( $( item ).siblings(".deviceScore")[0] ).find("span").text() + "." ); $( parentRow ).show(); }else{ $( parentRow ).hide(); } }); "";




Script#4

Last working executed on: 20210701_pm063845

Show only target:

var target = "IPHONE"; var ls = $( "div.col.deviceName.small-8" ); console.log(`size of $(ls.length)`); console.log( ls.length ); $.each( ls, function( index, item ){ var isTarget = $( item ).text().toUpperCase().includes( target ); var parentRow = $( item ).parent().parent().parent(); if( isTarget ) {console.log( $( item ).text() + "|" + isTarget + "|" + $( $( item ).siblings(".deviceScore")[0] ).find("span").text() + "." ); $( parentRow ).show(); }else{ $( parentRow ).hide(); } }); "";

Hide only target:

var target = "SHOWALLPHONE"; var ls = $( "div.col.deviceName.small-8" ); console.log(`size of $(ls.length)`); console.log( ls.length ); $.each( ls, function( index, item ){ var isTarget = $( item ).text().toUpperCase().includes( target ); var parentRow = $( item ).parent().parent().parent(); if( isTarget ) {console.log( $( item ).text() + "|" + isTarget + "|" + $( $( item ).siblings(".deviceScore")[0] ).find("span").text() + "." ); $( parentRow ).hide(); }else{ $( parentRow ).show(); } }); "";






Append column before Launch Price:

$( '<div class="col sort-header custom-small-2">test</div>' ).insertBefore( $( "div.col.launch_price" ) );






Google Referrals