Events in junit with Mockito - events

I try to test javax.enterprise.event.Event mocked by mockito but the following exception is thrown
Absent Code attribute in method that is not native or abstract in class file javax/enterprise/util/TypeLiteral
The class looks like
public class MyClassTest {
#Mock Event<MyAlarm> event;
//...
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
//...
}
#Test
public void myTest() {
MyClass myClass = new MyClass();
myClass.event = event;
//...
verify(event, never()).fire(any(MyAlarm.class));
//...
}
}
I add following to the pom.xml (maven project)
<repository>
<id>glassfish</id>
<name>GlassFish Maven Repository</name>
<url>http://maven.glassfish.org/content/groups/glassfish</url>
</repository>
and include the glassfish-embedded-all in front of the javax (javaee-web-api) dependency
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
What I have done wrong or what do I misunderstand?

I made some slight mistakes. This article Unit Testing for Java EE
by Adam Bien pointed me in the right direction.
I removed the javaee-web-api dependency and add the glassfish-embedded-all dependency. My pom.xml looks like:
<!-- .... -->
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.1</version>
<scope>provided</scope>
</dependency>
<!-- <dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- .... -->
<repositories>
<repository>
<url>http://download.java.net/maven/2/</url>
<id>java.net</id>
<layout>default</layout>
<name>java.net</name>
</repository>
</repositories>
This works well for me and I can test java ee events (or mock-it out).

As a workaround what I did was to add an inner class marked #Singleton in my test class that acts as an event observer. Such a class would look like this:
#Singleton
public static class MyEventObserver {
MyEvent event;
public void onMyEvent(#Observes MyEvent event) {
this.event = event;
}
}
}
And then in my test class I inject this class:
#Inject MyEventObserver observer;
and finally in the test method I would do something like:
Assert.assertNotNull(observer.event);
This worked for me.

The methods in TypeLiteral are final (see docs) which means that Mockito can not mock them (see the Mockito FAQ).
Perhaps consider using PowerMock (it can work in conjunction with Mockito I think) which apparently can mock final methods

Related

Spring Security context is always null in Spring Boot Cucumber Test

I have created a service with Spring Boot and Reactor. I try to test with Cucumber.
POM file (extract):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-console</artifactId>
<scope>test</scope>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
My cucumber configuration looks like this:
#Suite
class CucumberTestRunner
#CucumberContextConfiguration
#ContextConfiguration(classes = [CucumberBootstrap::class])
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = [MyApplication::class])
class CucumberSpringContextConfig
#ComponentScan("com.mypackage")
#Configuration
class CucumberBootstrap
The step definition I'm working on is like this:
#ScenarioScope
class MyTestStepDefs(
private val service: MyService
) {
#Given("User is authenticated")
fun userIsAuthenticated() {
val auth = UsernamePasswordAuthenticationToken(
User("username", "password", emptyList()),
"credentials",
emptyList<GrantedAuthority>()
)
val context = ReactiveSecurityContextHolder.getContext().block()
//ReactiveSecurityContextHolder.getContext()
// .doOnNext { it.authentication = auth }
// .block()
}
...
}
I would like to set the authenticated user into the security context. But unfortunately, the security context is always null.
I have also regular Springt Boot Tests running, where the security context is correctly returned. Here is an extract:
#SpringBootTest(webEnvironment = RANDOM_PORT)
#Transactional(propagation = Propagation.NOT_SUPPORTED)
class MyControllerTests #Autowired constructor(
private val client: WebTestClient
) {
#Test
#WithMockUser
fun `post instruction set`() {
val context = ReactiveSecurityContextHolder.getContext().block()
...
}
...
}
The service class is injected correctly into the step definitions class.
Does anyone have a hint what's missing in the configuration?
Thanks in advance for your help!
The tests that work use the #WithMockUser annotation which work (as described here) because this enables the WithSecurityContextTestExecutionListener.
Unfortunately you can't use this with Cucumber, Cucumber doesn't have a single test method. But ultimately this listener sets the context using:
this.securityContextHolderStrategyConverter.convert(testContext).setContext(supplier.get())
This then invokes (via some indirection):
TestSecurityContextHolder.setContext(context)
Which you could do inside Cucumber too. Though you may want to review the implementation of the WithSecurityContextTestExecutionListener for what exactly you're trying to reproduce.
Alternatively you can do your test end-to-end including the authentication.
Based on M.P. Korstanje's description, I implemented the step as follows:
#Given("User is authenticated")
fun userIsAuthenticated() {
val auth = UsernamePasswordAuthenticationToken(
User("username", "password", emptyList()),
"credentials",
emptyList<GrantedAuthority>()
)
val context = TestSecurityContextHolder.getContext()
context.authentication = auth
val newContext = ReactiveSecurityContextHolder.getContext().block()
assertEquals(auth, newContext?.authentication, "Wrong authentication stored in security context")
}

Maven + Surefire + #RunWith(JUnitPlatform.class) annotation

I have a Maven project where I want to run Junit 4 and 5 test cases. Some of the JUnit5 test cases also uses #RunWith(JUnitPlatform.class) annotation to run with Junit4. However, I have not been able to run both Junit 4 and 5 test cases in the same project so far.
See the code below. To compile JUnitPlatformClassDemoTest, I would need to add "junit-platform-runner" dependency in pom.xml, but the moment I add it, Maven ignores JUnit 5 test cases.
I have tried various combinations but nothing seems to work. Note that if I remove JUnitPlatformClassDemoTest class, then Maven is able to run the remaining Junit 4 and 5 test cases successfully. Problem seems to be only with #RunWith(JUnitPlatform.class) annotation.
How do I configure Maven such that it can run Junit 5 and 4 test cases and the tests which have #RunWith(JUnitPlatform.class) annotation ?
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>junit45</groupId>
<artifactId>junit45</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<!-- After adding this dependency JUnit 5 test cases are ignored -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
SampleTest class
package example;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class SampleTest {
List<String> list;
#BeforeEach
void beforeEach() {
list = new ArrayList<>();
}
#Test
public void join() {
list.add("foo");
list.add("bar");
Assertions.assertEquals("fooXbar", String.join("X", list));
}
#Test
public void unjoin() {
String joinedString = "fooXbar";
String[] values = joinedString.split("X");
Assertions.assertArrayEquals(new String[] { "foo", "bar" }, values);
}
}
JUnit4Test class
package example;
import org.junit.Assert;
import org.junit.Test;
public class JUnit4Test {
#Test
public void dummyTest() {
Assert.assertTrue(true);
}
}
JUnitPlatformClassDemoTest class
package example;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
public class JUnitPlatformClassDemoTest {
#Test
void successTest1() {
System.out.println("successTest1");
}
#Test
void successTest2() {
System.out.println("successTest2");
}
}

Spring Boot dont recognice Spring MVC

I am starting with SpringBoot, looks great, but I have some questions that I can't understand or find explained in the docs.
I created a new project with the Web, JPA, Security and MySQL dependencies. When my project is created, I go to create a #Controller class. Spring don't find #RequestMapping or ModelAndView classes.
I guessed that use the Web module of SpringBoot will add all the necessary dependencies to use SpringMVC (I read some examples and none add extra dependencies) and all work great with MVC.
These are my dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-groovy-templates</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Utils -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
</dependencies>
Other example, is the WebMvcConfigurerAdapter (from spring-mcv) class that I can't resolve:
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
This class is from, but I don't see Springboot include this dependy:
org.springframework
spring-webmvc
Maybe I am wrong and read some post that center all the info in Spring Boot, but don't show manual config in the poms.
WebApplication class (Auto Generated):
#SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
ServletInitializer.java class (Auto generated)
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
}
Looks like my maven local repository is corrupted.
I was using these libraries in other projects and all works fine. Sorry for the time wasted. Anyway, I let the response for some users with similar problems.

Configure integration test or use Spring JDBC in Unit Test using Spring Boot?

Am using the latest versions of Spring Boot, Spring JDBC, and Spring Rest...
My project is setup as a typical Maven project containing the following filesystem structure:
myproject
|
--src/main/java/com/myapp
--src/main/resource/application.properties
|
--src/test/java/com/myapp
--src/test/resources/application.properties
|
pom.xml
My application.properties are as follows (connecting to a local MySQL 5 database):
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.name=testdb
spring.datasource.initialize=true
spring.datasource.driverClassName=com.mysql.jdbc.Driver
MyDAO:
public interface MyDao {
public List<App> findAllApps();
}
MyDaoImpl:
#Repository("myDao")
public class MyDaoImpl implements MyDao {
#Autowired
JdbcTemplate jdbcTemplate;
public List<App> findAllApps() {
List<App> apps = this.jdbcTemplate.query(
"select app_name from app",
new RowMapper<App>() {
public App mapRow(ResultSet rs, int rowNum) throws SQLException {
App app = new App();
app.setAppName(rs.getString("app_name"));
return app;
}
});
return apps;
}
}
Its called in MyService class using Dependency Injection:
#RestController
public class MyService {
#Autowired
#Qualifier("myDao")
MyDao myDao;
#RequestMapping(value = "/apps", method = RequestMethod.GET, consumes = "text/plain", produces = "application/json")
public void process() throws JsonParseException, IOException {
List<App> apps = myDao.findAllApps();
System.out.println(apps.toString());
}
}
This totally works as stated in my RestController...
But however, in a typical JUnit test:
public class MyServiceTest {
#Autowired
#Qualifier("myDao")
MyDao myDao;
#Test
public void process() {
List<App> apps = myDao.findAllApps();
}
}
The call to myDao.findAllApps() returns a NullPointerException...
I even tried running my app (using the embedded tomcat) first by issuing the following from the command line:
mvn spring-boot:run
However a non-database specific JUnit test works inside Eclipse or when I do:
mvn clean install
Question(s):
How can I set it up so I can run an integration test and it actually hits my database (or a mock db for that matter) from MyServiceTest?
Why is the dependency injection failing when trying to inject in MyServiceTest for Spring JDBC?
Is there a way to setup my unit tests to test Rest calls?
Many thanks for everyone who took the time to read this and many many thanks for the people that respond!
Here's my pom.xml (per Eddu's request):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>myproject</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
It seems you aren't loading Spring's context in your test so the dependency injection isn't performed. You should do something like:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=Application.class) //Application being your
// Spring boot base config class
public class MyServiceTest { ... }
#Autowired
#Qualifier("myDao")
MyDao myDao;
You can have a look at an example in my github
Thanks everyone!
Steve - you were correct!
After much trial & tribulation, the #SpringApplicationConfiguration worked:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes=Application.class)
Eddu Melendez - I removed the spring-starter-data-jpa dependency with spring-boot-starter-jdbc and also removed the spring-jdbc dependency from my pom.xml file.
Thank you all for trying to help me solve this issue!

Mockito with static methods in util classes

I tried to find but couldnt get what i was looking for. Is it possible to do something like following in mockito?
when(TestServiceUtil.getTestItem()).thenReturn(someItem);
In your pom.xml, add the following dependencies:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.6</version>
<scope>test</scope>
</dependency>
above your test class:
#RunWith(PowerMockRunner.class)
public class YourClassName
[...]
#Before
public void beforeTest() throws SQLException {
PowerMockito.mockStatic(TestServiceUtil.class);
Now you can use (as you had it):
when(TestServiceUtil.getTestItem()).thenReturn(someItem);
Last words - don't overuse PowerMockito. Focus on clean, object oriented code.

Resources