Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

Give directions for creating a simple Java project in Intellij that incorporates JUnit 5…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-no-thinking’s answer.
  1. User

    Give directions for creating a simple Java project in Intellij that incorporates JUnit 5 for testing some of the methods in that project. Be very clear how the project structure should be created both for the project itself and for the JUnit tests. Give enough code and clear directions so that your instructions are directly implementable.

  2. Claude Assistant

    Creating a Simple Java Project with JUnit 5 in IntelliJ

    Step 1: Create a New Project

    1. Open IntelliJ IDEA
    2. Click on "New Project"
    3. Select "Java" from the left panel
    4. Set your Project SDK (JDK 11 or newer recommended)
    5. Click "Next"
    6. Name your project (e.g., "SimpleCalculator")
    7. Choose a location for your project
    8. Click "Finish"

    Step 2: Add JUnit 5 Dependencies

    1. Right-click on your project name in the Project Explorer
    2. Select "Open Module Settings" (or press F4)
    3. Go to "Libraries" tab
    4. Click the "+" button and select "From Maven"
    5. Search for "org.junit.jupiter:junit-jupiter:5.9.2" (or latest version)
    6. Check "Download to" option and click "OK"
    7. Select "Add to Module" and click "OK"
    8. Click "Apply" and then "OK"

    Step 3: Set Up Project Structure

    Create the following directory structure:

    SimpleCalculator/
      ├── src/
      │   ├── main/
      │   │   └── java/       (Source code)
      │   └── test/
      │       └── java/       (Test code)
    

    To create this structure:

    1. Right-click on "src" folder
    2. Select "New" > "Directory"
    3. Type "main/java" and click "OK"
    4. Again right-click on "src" folder
    5. Select "New" > "Directory"
    6. Type "test/java" and click "OK"
    7. Right-click on both directories and mark them accordingly:
      • Right-click on "java" under "main" > "Mark Directory as" > "Sources Root"
      • Right-click on "java" under "test" > "Mark Directory as" > "Test Sources Root"

    Step 4: Create a Simple Java Class

    1. Right-click on "main/java"
    2. Select "New" > "Package"
    3. Name it (e.g., "com.calculator")
    4. Right-click on the new package
    5. Select "New" > "Java Class"
    6. Name it "Calculator"
    7. Add the following code:
    package com.calculator;
    
    public class Calculator {
        
        public int add(int a, int b) {
            return a + b;
        }
        
        public int subtract(int a, int b) {
            return a - b;
        }
        
        public int multiply(int a, int b) {
            return a * b;
        }
        
        public double divide(int a, int b) {
            if (b == 0) {
                throw new ArithmeticException("Cannot divide by zero");
            }
            return (double) a / b;
        }
    }
    

    Step 5: Create a JUnit Test Class

    1. Right-click on "test/java"
    2. Select "New" > "Package"
    3. Use the same package name as your main class (e.g., "com.calculator")
    4. Right-click on the test package
    5. Select "New" > "Java Class"
    6. Name it "CalculatorTest"
    7. Add the following code:
    package com.calculator;
    
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.DisplayName;
    
    import static org.junit.jupiter.api.Assertions.*;
    
    class CalculatorTest {
        
        private Calculator calculator;
        
        @BeforeEach
        void setUp() {
            calculator = new Calculator();
        }
        
        @Test
        @DisplayName("Add two numbers")
        void testAdd() {
            assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
        }
        
        @Test
        @DisplayName("Subtract two numbers")
        void testSubtract() {
            assertEquals(1, calculator.subtract(3, 2), "3 - 2 should equal 1");
        }
        
        @Test
        @DisplayName("Multiply two numbers")
        void testMultiply() {
            assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
        }
        
        @Test
        @DisplayName("Divide two numbers")
        void testDivide() {
            assertEquals(1.5, calculator.divide(3, 2), "3 / 2 should equal 1.5");
        }
        
        @Test
        @DisplayName("Divide by zero")
        void testDivideByZero() {
            assertThrows(ArithmeticException.class, () -> calculator.divide(1, 0), 
                         "Dividing by zero should throw ArithmeticException");
        }
    }
    

    Step 6: Run the Tests

    1. Right-click on the "CalculatorTest" class in the Project Explorer
    2. Select "Run 'CalculatorTest'"

    Alternatively:

    • Click the green "play" button next to the class declaration
    • Or place your cursor inside the class and press Ctrl+Shift+F10 (Windows/Linux) or Control+Shift+R (Mac)

    To run a specific test method, right-click on the method name and select "Run".

    Step 7: View Test Results

    The test results will appear in the "Run" tool window at the bottom of the IDE:

    • Green checkmarks indicate passing tests
    • Red marks indicate failing tests

    You can click on a failed test to see the details of the failure.

    Would you like me to explain or break down the code?

  3. User

    There is no option 'Directory' in Intellj to create a new directory under the src folder.

  4. Claude Assistant

    You're absolutely right, my mistake. In IntelliJ, there's a different approach to create the directory structure. Here's the corrected method:

    Corrected Step 3: Set Up Project Structure

    1. Right-click on "src" folder
    2. Select "New" > "Package"
    3. Type "main.java" and click "OK"
    4. Right-click on "src" folder again
    5. Select "New" > "Package"
    6. Type "test.java" and click "OK"

    Once you've created these packages:

    1. Right-click on the "main.java" package
    2. Select "Mark Directory as" > "Sources Root"
    3. Right-click on the "test.java" package
    4. Select "Mark Directory as" > "Test Sources Root"

    Alternatively, you can use IntelliJ's built-in project structure template:

    1. Go to File > Project Structure
    2. Select "Modules" on the left
    3. Click on the "+" button and select "Add Content Root"
    4. Create "src/main/java" and "src/test/java" folders
    5. Mark them as "Sources" and "Tests" respectively using the buttons above the folder list

    Would this approach work better for you?

  5. User

    There is no option when right-clicking on main.java to mark directory as sources->root

  6. Claude Assistant

    I apologize for the confusion. You're right - the terminology and structure creation in IntelliJ works differently. Let me provide the correct approach:

    Corrected Project Structure Setup

    Method 1: Using Project Structure Settings

    1. Go to File > Project Structure (or press Ctrl+Alt+Shift+S)
    2. Select "Modules" on the left
    3. In the "Sources" tab, you'll see your project structure
    4. First create the directories if they don't exist:
      • Right-click on your project in the Project Explorer
      • Select New > Directory
      • Create "src/main/java" folder
      • Create "src/test/java" folder
    5. Back in Project Structure dialog:
      • Click on the "src/main/java" folder
      • Click the "Sources" button (blue folder icon) at the top
      • Click on the "src/test/java" folder
      • Click the "Tests" button (green folder icon) at the top
    6. Click "Apply" and "OK"

    Method 2: Alternative Approach

    1. Create a standard folder structure first:
      • Right-click on project > New > Directory > create "src"
      • Inside "src", create two directories: "main" and "test"
      • Inside each of those, create a "java" directory
    2. Then configure them:
      • Right-click on the "java" folder under main
      • Select "Mark Directory as" > "Sources Root"
      • Right-click on the "java" folder under test
      • Select "Mark Directory as" > "Test Sources Root"

    Does one of these approaches work with your version of IntelliJ?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.