2

I am new to Intellij IDEA. I'm trying to use jUnit annotations @Before and @After for my selenium tests. Even though I'm importing jUnit in my class, I 'm not able to use the annotations. Any help?

package Config;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;

public class browserConfig {

    public static WebDriver driver = null;
    public static Properties config = new Properties();
    public static FileInputStream fis;

    @Before
    public void initBrowser(){

        if (driver ==null){

            try{

                fis = new FileInputStream(System.getProperty("user.dir") + "//src//main//java//Config//config.properties");

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            }
UzIT
  • 51
  • 1
  • 7

2 Answers2

5

Your test should be in the test folder, otherwise idea will ignore the test annotations. Make sure the class is placed in the src/test/ folder. Also check in Project Settings -> Modules that your tests folder is present in the structure and has green color (marked as Tests). If it is missing - add it to the modules and mark as Tests.

GiorgosDev
  • 1,757
  • 1
  • 14
  • 16
  • and who says it isn't in the test source folder? – Stultuske Jun 18 '18 at 13:24
  • Please check if IDEA recognized your folder as test, as I described in my update. This is a standard behavior when you are trying to run a test from non-test folders. – GiorgosDev Jun 18 '18 at 13:26
  • @user2529284 I have structured my code in such a way that I have configuration file in main that would have Before and After methods and i will use Test in my tests that will reside in my test folder – UzIT Jun 18 '18 at 13:38
  • @UzIT you need to separate tests from application, it is a good practice in the first place and IDEA will not allow you to run any test-related annotations from non-test folders. You should move before and after methods to corresponding test class – GiorgosDev Jun 18 '18 at 13:48
  • 1
    @user2529284 if it is a gradle project, make sure to refresh gradle dependencies – Pooya Jun 18 '18 at 14:50
  • Perfect. You helped me solve all my import issues on a different matter! And here this question was -1! – chrips Mar 01 '19 at 06:12
0

If you are using gradle to build in Intellij, try adding this to build.gradle.

testCompile('org.springframework.boot:spring-boot-starter-test')

If you aren't using gradle do something similar with whatever controls your dependencies.

Terry Horner
  • 497
  • 6
  • 16
  • Should be `testImplementation`, otherwise you'll get a error message `Could not find method testCompile()`. See [Build error with gradle Could not find method testCompile()](https://stackoverflow.com/questions/38385680/build-error-with-gradle-could-not-find-method-testcompile) for more information. – Semo Aug 06 '23 at 16:11