I am trying to compile a java project. I get the following error from my code. What am I doing wrong?
import java.lang.reflect.Field;
import java.util.Map;
import java.lang.Object;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import java.lang.String;
import org.junit.Test;
public class EnvironmentsTest {
#Test
public void testGetFoobar() throws Exception {
assertNull(System.getenv("MY_VAR"));
injectEnvironmentVariable("MY_VAR", "my_var");
assertThat(System.getenv("MY_VAR"), is("my_var"));
}
cannot find symbol
symbol: method is(java.lang.String)
location: class EnvironmentsTest
is is a matcher from Hamcrest. See http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/core/Is.html
import is:
import static org.hamcrest.CoreMatchers.is;
Type is () and press Alt+Enter to click "import static method.."
You can add CoreMatchers.is.
like this
Related
This is the #before annotation that I can't solve
#Before
public void initDb() {
{
User newUser = new User("testUser#mail.com", "testUser", "****");
userService.createUser(newUser);
}
{
User newUser = new User("testAdmin#mail.com", "testUser", "****");
userService.createUser(newUser);
}
}
Below are all my imports
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.leadApp.Entities.Task;
import com.leadApp.Entities.User;
import com.leadApp.services.TaskService;
import com.leadApp.services.UserService;
I would appreciate if someone can give an insight of what next I can do, I am using eclipse Oxygen
You seem to be using JUnit 5 AKA JUnit Jupiter.
The annotation is named BeforeEach, not Before. And of course, it should be imported from an org.junit.jupiter package, not from org.aspectj.lang.annotation, which has nothing to do with JUnit.
There is a guide, you should read it: https://junit.org/junit5/docs/current/user-guide/
"File" is underlined with swiggly lines. How would I fix this?
I am really confused since I have the correct imports don't I? Tried searching for answers online and couldn't find it.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.FileInputStream;
import org.junit.Test;
#Test
public void testLogin() {
System.setIn(new FileInputStream(new File("input.txt")));
}
}
I am aware a question with the exact same title exists but the answer doesn't help me any further.
I am using WebMvcTest to test my controller class. However when it comes to comparing the result using .andExpect, my IDEA (intellij) can't resolve it.
The question with the same title as this one's solution is included in my imports and is unused. I also looked at the spring docs and used the 2 implements needed.
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
Below you will find my entire class and in this case all my imports.
package be.pxl.backend.restapi;
import be.pxl.backend.restapi.controller.UserController;
import be.pxl.backend.restapi.domain.User;
import be.pxl.backend.restapi.manager.UserManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.*;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
//stackoverflow try == unused
import org.springframework.test.web.servlet.ResultActions;
#RunWith(SpringRunner.class)
#WebMvcTest(UserController.class)
public class UserControllerIntegrationTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private UserManager userManager;
#Test
public void givenUser_WhenGetUser_ThenReturnUser() throws Exception{
User bjorn = new User();
bjorn.setUsername("Bjorn");
bjorn.setPassword("Wachtwoord");
bjorn.setEmail("test#email.be");
given(userManager.getUserById(1L)).willReturn(bjorn);
mockMvc.perform(get("/user/1")
.contentType(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].username", is(bjorn.getUsername()))));
}
}
First of all, according to the Checkstyle, you should avoid star imports.
Secondly, you have misplaced two closing parentheses, one in contentType() and the other one in the last call of andExpect(). Below is a working code.
imports:
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import be.pxl.backend.restapi.controller.UserController;
import be.pxl.backend.restapi.domain.User;
import be.pxl.backend.restapi.manager.UserManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
Note the imports are ordered as per Google Java Style.
mock test:
mockMvc.perform(get("/user/1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].username", is(bjorn.getUsername())));
I want to use Robolectric for Unit Testing but I am trying a simple test with robolectric and I am stuck at the beginning. I followed the manual, I did the same with the examples and even other posts couldn't help me. Every time I get the error message: cannot access path. class file for java.nio.file.Path not found.
My build.gradle is:
testCompile "org.robolectric:robolectric:3.3.2"
testCompile "org.robolectric:shadows-support-v4:3.3.2"
testCompile 'junit:junit:4.12'
My Test class is:
package com.dev.mann.chronoly;
import android.support.v7.widget.RecyclerView;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowToast;
import org.robolectric.shadows.support.v4.SupportFragmentTestUtil;
import static
org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;
#RunWith(RobolectricTestRunner.class)
#Config(constants = BuildConfig.class)
public class CrngyMasterFragmentTestClass{
private CrngyMasterFragment mFrag;
RecyclerView mMainRecyclerView;
MainActivity activity;
#Before
public void setup() {
activity = Robolectric.buildActivity(MainActivity.class).create().start().visible().get();
//SupportFragmentTestUtil.startVisibleFragment(mFrag, MainActivity.class, R.id.master_frag_container);
}
#Test
public void checkEmptyFragments() throws Exception {
//SupportFragmentTestUtil.startVisibleFragment(mFrag, AppCompatActivity.class, R.id.master_frag_container);
assertThat(true);
// check the recyclerview items
//RecyclerView recyclerView = (RecyclerView) mFrag.getActivity().findViewById(R.id.mainRecyclerView);
//assertThat(recyclerView.isShown());
}
}
But every settings doesn't work.
Any help is much appreciated, thanks for any help.
From the comments, the solution was to replace the line
import static org.assertj.core.api.Assertions.assertThat;
with
import static org.assertj.core.api.Java6Assertions.assertThat;
as per the AssertJ documentation.
I am having trouble running tests in a subclass in IntelliJ. This seems to work when I run the build through maven, but trying to run the child class in IntelliJ causes the child test to get ignored. Here is an example:
The parent class:
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.fail;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class TestSomething {
#Test
public void runSomething(){
//Test Stuff
}
}
And the child class:
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class TestThisSomethingAsWell extends TestSomething{
#Test(dependsOnMethods = "runSomething")
public void runSomethingElse(){
//More Test Stuff
}
}
In the above example, both runSomething and runSomethingElse get called when running through Maven, but when I run the TestThisSomethingAsWell class through IntelliJ only runSomething is run. It does not even show runSomethingElse in the list of tests.
Also note that these are selenium tests, but I am not sure if that is part of the problem or not.
Edit: Added import statements.